MongoDB Delete Multiple Documents Using MongoDB Shell (original) (raw)

Last Updated : 31 Jan, 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

**What is 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.

**Key Features of deleteMany()

**Syntax

**db.collection.deleteMany(
,
{
writeConcern: ,
collation:
}
)

**1. Parameters

**2. Optional Parameters:

**3. Return:

The method returns a document containing:

**Example 1: Deleting Documents That Match a Filter

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:

**Sample Documents in contributors Collection:

sample database and 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:

deleting multiple documents example

**Explanation:

**Example 2: Deleting All Documents in a Collection

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 from contributors collection

db.contributor.deleteMany({})

**Output

delete all documents example output

**Explanation:

**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.

**Example 3: Using Query Operators with deleteMany()

We can use **query operators to refine deletions. Let’s delete all contributors whose _id is greater than 2.

**Query: Delete contributors with _id greater than 2

db.contributors.deleteMany({ _id: { $gt: 2 } });

**Output:

{ "acknowledged" : true, "deletedCount" : 2 }

**Explanation:

**Best Practices for Using 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.

**1. Always Use a Filter

db.users.deleteMany({});  // Deletes ALL users!  

**2. Use Indexes for Faster Deletion

**3. Test Queries Before Executing

db.contributors.find({ language: "C#" });  

**4. Consider Write Concern for Critical Operations

db.orders.deleteMany({ status: "Cancelled" }, { writeConcern: { w: "majority" } });  

**5. Use Collation for Case-Insensitive Deletion

db.users.deleteMany(

{ name: "john doe" },

{ collation: { locale: "en", strength: 2 } }

);

Conclusion

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