For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Delete Documents

In MongoDB, a Delete operation consists of deleting documents from a collection. This page provides examples.

You can delete documents in MongoDB using the following methods:


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


This page uses the following mongosh methods:

The examples on this page use the inventory collection. To populate the inventory collection, run the following:

db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
] );

To delete all documents from a collection, pass an empty filter document {} to the db.collection.deleteMany() method.

The following example deletes all documents from the inventory collection:

db.inventory.deleteMany({})

The method returns a document with the status of the operation. For more information and examples, see deleteMany().

You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.

To specify equality conditions, use <field>:<value> expressions in the query filter document:

{ <field1>: <value1>, ... }

A query filter document can use the query operators to specify conditions in the following form:

{ <field1>: { <operator1>: <value1> }, ... }

To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany() method.

The following example removes all documents from the inventory collection where the status field equals "A":

db.inventory.deleteMany({ status : "A" })

The method returns a document with the status of the operation. For more information and examples, see deleteMany().

To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the db.collection.deleteOne() method.

The following example deletes the first document where status is "D":

db.inventory.deleteOne( { status: "D" } )

Note

You can delete only one document at a time in the MongoDB Atlas UI. To delete multiple documents, connect to your Atlas deployment from mongosh or a MongoDB driver and follow the examples on this page for your preferred method.

The example in this section uses the sample movies dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.

To delete a document in MongoDB Atlas, follow these steps:

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

    The Clusters page displays.

2
  1. For the cluster that contains the sample data, click Browse Collections.

  2. In the left navigation pane, select the sample_mflix database.

  3. Select the movies collection.

3

Optionally, you can specify a query filter document in the Filter field. A query filter document uses query operators to specify search conditions.

Copy the following query filter document into the Filter search bar and click Apply:

{ genres: "Action", rated: { $in: [ "PG", "PG-13" ] } }

This query filter returns all documents in the sample_mflix.movies collection where genres equals Action and rated equals either PG or PG-13.

4
  1. For the document that you want to delete, hover over the document and click the trash icon that appears on the right-hand side.

    After clicking the delete button, MongoDB Atlas flags the document for deletion and asks for your confirmation.

  2. Click Delete to confirm your selection.

To learn more, see Create, View, Update, and Delete Documents.

Delete operations do not drop indexes, even if deleting all documents from a collection.

Note

Atomicity and Write Acknowledgement

MongoDB write operations are atomic on the level of a single document. For more information, see Atomicity and Transactions.

You can also use write concerns to specify the level of acknowledgment requested from MongoDB for write operations. For more information, see Write Concern.