MongoDB Delete Document

MongoDB Delete Document means removing one document, many matching documents, or every document from a MongoDB collection. In this MongoDB Tutorial, we shall learn how to delete MongoDB Document(s) using the current deleteOne() and deleteMany() methods, and also understand the older remove() method used in legacy examples.

For new MongoDB shell examples, prefer db.collection.deleteOne() to delete a single document and db.collection.deleteMany() to delete all documents that match a filter. The older db.collection.remove() method is kept in this tutorial because it appears in many existing MongoDB examples, but current MongoDB documentation recommends replacing it with deleteOne() or deleteMany().

MongoDB delete document methods: deleteOne(), deleteMany(), and remove()

MongoDB methodUse this whenTypical filter
deleteOne()You want to delete only the first document that matches the filter. Use a unique field such as _id when the deletion must be precise.{ _id: ObjectId("...") }
deleteMany()You want to delete every document that matches a filter.{ age: 23 }
deleteMany({})You want to delete all documents in a collection but keep the collection and its indexes.{}
remove()You are reading or maintaining older MongoDB shell code.{ age: 23 }
drop()You want to remove the whole collection itself, including its indexes.Not a document delete filter

MongoDB official reference pages for these methods are useful when you need version-specific behavior: Delete Documents, deleteOne(), deleteMany(), and remove().

Safe workflow before deleting MongoDB documents

Deleting documents is permanent unless you have a backup, restore point, or application-level recovery plan. A safe MongoDB delete workflow is:

  1. Prepare a filter that selects only the documents to delete.
    const filter = { field1: value1, field2: value2 }
  2. Preview the matching documents before deleting them.
    db.collection.find(filter)
  3. Count the matching documents when the collection is large.
    db.collection.countDocuments(filter)
  4. Run deleteOne(filter) or deleteMany(filter).
  5. Verify the result using deletedCount and, if required, query the collection again.

deleteOne() syntax to delete one MongoDB document

Use deleteOne() when only one document should be removed. If the filter matches more than one document, MongoDB deletes only the first matching document, so a unique filter such as _id is usually the safest choice.

</>
Copy
db.collection.deleteOne(filter, options)

Example using a unique _id value:

</>
Copy
db.customers.deleteOne({
  _id: ObjectId("59edda4b5f82df4555f2bfa7")
})
{
  acknowledged: true,
  deletedCount: 1
}

deleteMany() syntax to delete matching MongoDB documents

Use deleteMany() when every document matching a filter should be removed. This method returns a result object with deletedCount, which tells how many documents were deleted.

</>
Copy
db.collection.deleteMany(filter, options)

Example to delete all customers whose age is 23:

</>
Copy
db.customers.deleteMany({ age: 23 })
{
  acknowledged: true,
  deletedCount: 2
}

Delete all documents in a MongoDB collection with deleteMany({})

To delete all documents from a collection, pass an empty filter document {} to deleteMany(). This removes the documents but keeps the collection, indexes, and collection options.

</>
Copy
db.customers.deleteMany({})
{
  acknowledged: true,
  deletedCount: 4
}

If your intention is to remove the collection itself, use db.customers.drop() instead of deleting each document. Use drop() only when you also want the collection structure removed.

Legacy syntax of db.collection.remove() method

The following section explains the older remove() method. It is useful for understanding legacy tutorials and old shell scripts. For new code, map remove(criteria) to deleteMany(criteria), and map remove(criteria, true) or remove(criteria, 1) to deleteOne(criteria).

Following is the syntax of remove() method to delete documents from a collection in MongoDB :

db.collection_name.remove(CRITERIA, JUST_ONE)

where

collection_nameString[mandatory] Name of the Collection from which you would like to remove Documents
CRITERIADocument[optional] The criteria that selects the required documents to delete
JUST_ONE1 or true[optional] If one or true, deletes only one document from the selection

Example to delete MongoDB documents based on criteria

In this example, we shall delete only some documents from a customers collection using a filter condition. Following is the collection we consider in this example.

> db.customers.find({})
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa6"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa7"), "name" : "Akhil", "age" : 24, "place" : "New York" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa8"), "name" : "Honey", "age" : 25 }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa9"), "name" : "Manju", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfaa"), "name" : "Bharat", "age" : 24, "place" : "New York" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfab"), "name" : "Arya", "age" : 25 }

Now we shall form a criteria,

> criteria = {"age" : 23}
{ "age" : 23 }

Check the entries that could be selected using the criteria

> db.customers.find(criteria)
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa6"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa9"), "name" : "Manju", "age" : 23, "place" : "Amaravati" }

There are two entries for this criteria. Once we are sure that we can delete these documents, delete them using remove() method as shown below :

> db.customers.remove(criteria)
WriteResult({ "nRemoved" : 2 })

MongoDB acknowledged with result of the Operation, { “nRemoved” : 2 } , saying two documents are deleted.

The same deletion using the current deleteMany() method would be:

</>
Copy
db.customers.deleteMany({ age: 23 })

Example to delete all MongoDB documents present in the collection

In this example, we shall delete all documents present in a collection named customers .

> db.customers.count()
4

There are four documents in the collection. In current mongosh, you can use db.customers.countDocuments() for an exact document count.

To delete all documents in the collection using the older method, run db.collection.remove() with an empty query.

> db.customers.remove({})
WriteResult({ "nRemoved" : 4 })

All the documents were deleted from the collection.

> db.customers.count()
0

The current method for the same operation is:

</>
Copy
db.customers.deleteMany({})

Example to delete only one MongoDB document when many documents match

For the following criteria there are two documents.

> criteria={age:25}
{ "age" : 25 }
> db.customers.find(criteria).count()
2

Now we shall delete only one document using justOne argument of remove() method

> db.customers.remove(criteria,1)
WriteResult({ "nRemoved" : 1 })

Despite multiple Documents for the criteria, only one document is deleted because of the justOne argument. And the rest of the documents continue to exist in the collection.

> db.customers.find(criteria).count()
1

The current method for this operation is deleteOne():

</>
Copy
db.customers.deleteOne({ age: 25 })

When more than one document can match the filter, use deleteOne() only if deleting any one matching document is acceptable. When you must delete a specific customer, include a unique field such as _id.

Delete a MongoDB document in Compass

In MongoDB Compass, open the database and collection, go to the Documents view, find the document, and use the document delete option from the document row or context menu. Before confirming, check the document fields carefully because Compass performs the same kind of database deletion as a shell command.

Compass is convenient for one-off manual deletion. For repeatable application tasks, migration scripts, cleanup jobs, or production maintenance, prefer a reviewed shell command or driver code where the filter can be tested and version controlled.

Common mistakes while deleting MongoDB documents

  • Using an empty filter by accident: deleteMany({}) deletes every document in the collection.
  • Using deleteOne() with a broad filter: deleteOne({ age: 25 }) deletes only the first match, which may not be the document you intended.
  • Forgetting to preview the filter: Always run find(filter) or countDocuments(filter) before deleting important data.
  • Confusing deleteMany({}) with drop(): deleteMany({}) keeps the collection; drop() removes the collection.
  • Depending only on old remove() examples: Use deleteOne() and deleteMany() for new MongoDB shell examples.

QA checklist for this MongoDB delete document tutorial

  • Does the tutorial clearly distinguish deleteOne(), deleteMany(), remove(), and drop()?
  • Does every delete example show a specific filter or explicitly explain the empty filter {}?
  • Does the article warn readers to preview documents before deletion?
  • Are legacy remove() examples marked as older usage while still preserving their original behavior?
  • Does the FAQ answer the common MongoDB delete questions without adding unsupported version claims?

MongoDB delete document FAQs

How do I delete all documents in a MongoDB collection?

Use db.collection.deleteMany({}). The empty filter {} matches all documents. This deletes the documents but keeps the collection and indexes.

What does deleteOne() do in MongoDB?

deleteOne() deletes the first document that matches the filter. For precise deletion, use a unique value such as _id in the filter.

Which MongoDB method is used to remove many documents from a collection?

Use deleteMany(filter) to remove all documents that match a filter. Older examples may use remove(filter), but deleteMany() is the current method to prefer.

How do I delete a document in MongoDB Compass?

Open the collection in Compass, switch to the Documents view, locate the document, choose the delete option for that document, and confirm the deletion after checking the fields.

Is deleteMany({}) the same as dropping a MongoDB collection?

No. deleteMany({}) removes all documents from the collection, while drop() removes the collection itself along with its indexes.

Summary of MongoDB delete document operations

In this MongoDB Tutorial – MongoDB Delete Document(s), we have learnt to delete one document using deleteOne(), delete multiple matching documents using deleteMany(), delete all documents using deleteMany({}), and understand the older remove() method with examples.