diff --git a/lisp/eshell/em-hist.el b/lisp/eshell/em-hist.el
index 4081f2a7f85..e7665de3ab7 100644
--- a/lisp/eshell/em-hist.el
+++ b/lisp/eshell/em-hist.el
@@ -385,10 +385,12 @@ eshell-add-input-to-history
              (if (eq eshell-hist-ignoredups 'erase)
                  ;; Remove any old occurrences of the input, and put
                  ;; the new one at the end.
-                 (unless (ring-empty-p eshell-history-ring)
-                   (ring-remove eshell-history-ring
-	                        (ring-member eshell-history-ring input))
-                   t)
+                 (or (unless (ring-empty-p eshell-history-ring)
+                       (let (index)
+                         (while (setq index (ring-member eshell-history-ring input))
+                           (ring-remove eshell-history-ring index)))
+                       t)
+                     t)
                ;; Always add...
                (or (null eshell-hist-ignoredups)
                    ;; ... or add if it's not already present at the
diff --git a/test/lisp/eshell/em-hist-tests.el b/test/lisp/eshell/em-hist-tests.el
index d325e3a6402..04d7b1a0670 100644
--- a/test/lisp/eshell/em-hist-tests.el
+++ b/test/lisp/eshell/em-hist-tests.el
@@ -33,6 +33,51 @@ eshell-write-readonly-history
                    (propertize "echo bar" 'read-only t))
       (eshell-write-history histfile))))
 
+(ert-deftest eshell-add-history-element-to-empty-ring-with-erase-dups ()
+  "Test that we add a new history item to an empty history ring with `eshell-hist-ignoredups' set to 'erase."
+  (ert-with-test-buffer (:name "eshell-hist-test")
+    (let ((eshell-history-ring (make-ring 2))
+          (eshell-hist-ignoredups 'erase))
+      (eshell-add-input-to-history "echo foo")
+      (should (= (ring-length eshell-history-ring) 1))
+      (should (= (ring-member eshell-history-ring "echo foo") 0)))))
+
+(ert-deftest eshell-add-history-element-to-non-empty-ring-with-erase-dups-erases-duplicate ()
+  "Test that when we add an existing history item to a non empty history ring with `eshell-hist-ignoredups' set to 'erase the existing element is erased."
+  (ert-with-test-buffer (:name "eshell-hist-test")
+    (let ((eshell-history-ring (make-ring 2))
+          (eshell-hist-ignoredups 'erase))
+      (eshell-add-input-to-history "echo foo")
+      (eshell-add-input-to-history "echo foo")
+      (should (= (ring-length eshell-history-ring) 1))
+      (should (= (ring-member eshell-history-ring "ls") 0)))))
+
+(ert-deftest eshell-add-history-element-to-non-empty-ring-with-erase-dups-erases-duplicates ()
+  "Test that when we add an existing history item to a non empty history ring with `eshell-hist-ignoredups' set to 'erase the existing elements are erased."
+  (ert-with-test-buffer (:name "eshell-hist-test")
+    (let ((eshell-history-ring (make-ring 5))
+          (eshell-hist-ignoredups nil))
+      (eshell-add-input-to-history "echo foo")
+      (eshell-add-input-to-history "echo bar")
+      (eshell-add-input-to-history "echo foo")
+      (should (= (ring-length eshell-history-ring) 3))
+      (setq eshell-hist-ignoredups 'erase)
+      (eshell-add-input-to-history "echo foo")
+      (should (= (ring-length eshell-history-ring) 2))
+      (should (= (ring-member eshell-history-ring "echo foo") 0))
+      (should (= (ring-member eshell-history-ring "echo bar") 1))))))
+
+(ert-deftest eshell-add-history-element-to-non-empty-ring-with-erase-dups-keeps-non-duplicates ()
+  "Test that when we add a new  history item to a non empty history ring with `eshell-hist-ignoredups' set to 'erase the existing elements are not erased."
+  (ert-with-test-buffer (:name "eshell-hist-test")
+    (let ((eshell-history-ring (make-ring 2))
+          (eshell-hist-ignoredups 'erase))
+      (eshell-add-input-to-history "echo foo")
+      (eshell-add-input-to-history "echo bar")
+      (should (= (ring-length eshell-history-ring) 2))
+      (should (= (ring-member eshell-history-ring "echo foo") 1))
+      (should (= (ring-member eshell-history-ring "echo bar") 0)))))
+
 (provide 'em-hist-test)
 
 ;;; em-hist-tests.el ends here
