Last Updated : 12 Jul, 2025
MongoDB provides powerful and flexible methods for managing data, including the ability to delete multiple documents efficiently. The db.collection.deleteMany()
method allows users to remove multiple documents that match a specified filter, making it an essential tool for database maintenance and data manipulation.
In this article, we will cover everything you need to know about deleting multiple documents in MongoDB, from basic syntax to advanced filtering techniques. This guide is structured to help us understand how to use deleteMany()
effectively, its performance implications, and best practices to ensure smooth database operations
deleteMany()
in MongoDB?
The db.collection.deleteMany() method is used to delete multiple documents from a collection that match a specified filter in Mongo Shell. Unlike deleteOne()
, which removes only a single document, deleteMany()
can remove multiple documents at once, making it ideal for bulk deletions. This method can be used in multi-document transactions. If we use this method in a capped collection, then it will throw an exception.
deleteMany()
Syntax
1. Parametersdb.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
{}
is passed, all documents in the collection will be deleted.The method returns a document containing:
acknowledged
- true
if the write concern is enabled, false
otherwise.deletedCount
- The total number of documents deleted.We have a MongoDB database named GeeksforGeeks
, which contains a collection called contributors
. This collection stores details about contributors in the form of documents with the following structure:
contributors
Collection:
In this example, we are deleting multiple documents from the contributor collection that match the filter, i.e., language: "C#".
Query: Delete all contributors who use C#db.contributors.deleteMany({ language: "C#" });
Output:
Explanation:{ language: "C#" }
matches two documents.deletedCount: 2
, confirming that two documents were removed successfully.In this example, we are deleting all the documents from the contributor collection by passing empty documents in the db.collection.deleteMany() method.
Query: Delete all documents fromcontributors
collection
db.contributor.deleteMany({})
Output
Explanation:{}
, it matches all documents.contributors
collection is deleted.Warning: This operation removes all documents permanently. If you need to clear a collection but keep its structure, use deleteMany({})
instead of drop()
, which removes the collection entirely.
deleteMany()
We can use query operators to refine deletions. Let's delete all contributors whose _id
is greater than 2.
_id
greater than 2
db.contributors.deleteMany({ _id: { $gt: 2 } });
Output:
{ "acknowledged" : true, "deletedCount" : 2 }
Explanation:
{ _id: { $gt: 2 } }
selects documents where _id
is greater than 2.deletedCount: 2
.deleteMany()
in MongoDB
When using the deleteMany()
method in MongoDB, it's crucial to ensure that only the intended documents are removed to prevent accidental data loss. Implementing best practices can help maintain data integrity and improve query efficiency.
db.users.deleteMany({}); // Deletes ALL users!
userID
, ensure an index exists to speed up deletion.db.contributors.find({ language: "C#" });
deleteMany()
only when sure.db.orders.deleteMany({ status: "Cancelled" }, { writeConcern: { w: "majority" } });
Conclusiondb.users.deleteMany(
{ name: "john doe" },
{ collation: { locale: "en", strength: 2 } }
);
The deleteMany()
method is an essential tool in MongoDB for bulk deletion of documents based on various filters. Understanding its syntax, behavior, and best practices helps in efficiently managing databases without accidental data loss. By following optimized query patterns, utilizing indexes, and testing queries before execution, you can ensure safe and effective deletions in MongoDB. With these examples and techniques, you now have a comprehensive understanding of how to delete multiple documents using MongoDB Shell
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4