🍃 MongoDB Tutorial

Delete Documents in MongoDB: deleteOne, deleteMany & remove()

Learn how to delete documents in MongoDB with deleteOne() and deleteMany(), and the difference from the legacy remove() method.

Delete Documents in MongoDB

MongoDB gives you two main methods for removing documents from a collection: deleteOne() and deleteMany(). Both accept a query object — every matching document (or the first, for deleteOne) gets permanently removed.

deleteOne()

Deletes only the first document that matches the query.

db.employees.deleteOne({ empid: 210 })
// { acknowledged: true, deletedCount: 1 }

deleteMany()

Deletes every document that matches the query.

db.employees.deleteMany({ city: "Mumbai" })
// { acknowledged: true, deletedCount: 4 }

db.employees.deleteMany({ city: "Pune" })
// { acknowledged: true, deletedCount: 3 }

Passing an empty query object {} to deleteMany() deletes every document in the collection — use it carefully.

remove() (Legacy)

Older code may still use db.collection.remove(condition), which deletes matching documents based on a filter. It predates deleteOne()/deleteMany() and is considered legacy in modern MongoDB drivers — prefer the two methods above in new code.

Common Errors & Troubleshooting

  • Accidentally deleting everything with deleteMany({}) — an empty filter object matches every document. Always double-check the filter with a find() first if you're unsure how many documents will be affected.
  • Expecting deleteOne() to remove all matches — it only removes the first match found; if you meant to remove every matching document, use deleteMany() instead.
  • deletedCount: 0 — no documents matched the filter; this usually means a typo in the field name/value, not that the delete "failed."

Key Takeaways

  • deleteOne() stops after the first match; deleteMany() removes every match.
  • An empty filter {} matches (and can delete) the entire collection — double-check your query before running it.
  • remove() still works but is superseded by deleteOne()/deleteMany().