Fix This Page
Navigation

Delete Documents

This page provides examples of delete operations using the following methods in the mongo shell:

This page provides examples of delete operations using the following methods in the PyMongo Python driver:

This page provides examples of delete operations using the following methods in the MongoDB PHP Library:

This page provides examples of delete operations using the following methods in the MongoDB Perl Driver:

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" },
]);

You can run the operation in the web shell below:

db.inventory.insert_many([
    {"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"}])
$insertManyResult = $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',
    ],
]);
$db->coll("inventory")->insert_many(
    [
        {
            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"
        }
    ]
);

Delete All Documents

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

To remove all documents from a collection, pass an empty filter document {} to the pymongo.collection.Collection.delete_many() method.

To remove all documents from a collection, pass an empty filter document [] to the MongoDB\Collection::deleteMany() method.

To remove all documents from a collection, pass an empty filter document {} to the MongoDB::Collection::delete_many() method.

The following example deletes all documents from the inventory collection:

db.inventory.deleteMany({})
db.inventory.delete_many({})
$deleteResult = $db->inventory->deleteMany([]);
$db->coll("inventory")->delete_many( {} );

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

The delete_many() method returns an instance of pymongo.results.DeleteResult with the status of the operation.

Upon successful execution, the deleteMany() method returns an instance of MongoDB\DeleteResult whose getDeletedCount() method returns the number of documents that matched the filter.

Upon successful execution, the delete_many() method returns an instance of MongoDB::DeleteResult whose deleted_count attribute contains the number of documents that matched the filter.

Delete All Documents that Match a Condition

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

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

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

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

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

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

db.inventory.deleteMany({ status : "A" })
db.inventory.delete_many({"status": "A"})
$deleteResult = $db->inventory->deleteMany(['status' => 'A']);
$db->coll("inventory")->delete_many( { status => "A" } );

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

The delete_many() method returns an instance of pymongo.results.DeleteResult with the status of the operation.

Upon successful execution, the deleteMany() method returns an instance of MongoDB\DeleteResult whose getDeletedCount() method returns the number of documents that matched the filter.

Upon successful execution, the delete_many() method returns an instance of MongoDB::DeleteResult whose deleted_count attribute contains the number of documents that matched the filter.

Remove Only One Document that Matches a Condition

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.

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

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

To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the MongoDB::Collection::delete_one() method.

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

db.inventory.deleteOne( { status: "D" } )
db.inventory.delete_one({"status": "D"})
$deleteResult = $db->inventory->deleteOne(['status' => 'D']);
$db->coll("inventory")->delete_one( { status => "D" } );

Delete Behavior

Indexes

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

Atomicity

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