MongoDB -deleteMany() Method

Last Updated : 5 Feb, 2026

The deleteMany() method in MongoDB is used to remove all documents from a collection that match a specified filter. It is ideal for bulk deletions, supports multi-document transactions, and ensures efficient data management.

  • Deletes multiple documents that match a specified filter.
  • Returns a result containing the number of deleted documents.
  • Cannot be used on capped collections.
  • Supports write concern and collation options.

Syntax

db.collection.deleteMany(<filter>, {writeConcern: <document>, collation: <document>})
  • filter: Selection criteria for deletion, {} deletes all documents.
  • writeConcern: Sets the write concern level for the operation.
  • collation: Defines language-specific rules for string comparison.
  • acknowledged: true if the write concern is enabled, false otherwise.
  • deletedCount: The total number of documents deleted.

Return Value of deleteOne()

The deleteOne method returns a document containing:

  • acknowledged: A boolean indicating whether the operation was acknowledged based on the write concern.
  • deletedCount: The number of documents deleted (should always be 1 if a match is found).

Examples of MongoDB deleteOne() Method

In the following examples we are working with:

  • Database: GeeksforGeeks
  • Collection: contributor
  • Document: Four documents that contain the details of the contributors in the form of field-value pairs.

Screenshot-2026-02-05-104059

Example 1: Deleting Documents That Match a Filter

Deletes all documents in which language field field contains C#.

Query:

db.contributor.deleteMany({ language: "C#" });

Output:

Screenshot-2026-02-05-104457

  • The query { language: "C#" } matches two documents.
  • Both matching documents are deleted.
  • The operation returns deletedCount: 2, confirming that two documents were removed successfully.

Example 2: Deleting All Documents in a Collection

Deleting all the documents from the contributor collection by passing empty documents in the db.collection.deleteMany() method.

Query:

db.contributor.deleteMany({})

Output:

Screenshot-2026-02-05-104646

  • Since the filter is {}, it matches all documents.
  • Every document in the contributors collection is deleted.
  • The output confirms the number of deleted documents.

Note: 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()

Delete all contributors whose _id is greater than 2.

Query:

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

Output:

Screenshot-2026-02-05-105303
  • The condition { _id: { $gt: 2 } } selects documents where _id is greater than 2.
  • Two matching documents are found and deleted.
  • The operation confirms deletion with deletedCount: 2.

Best Practices for Using deleteMany() in MongoDB

Here are some best practice discussed:

  • Always use a filter: An empty {} deletes all documents.
  • Use indexes: Index the filter field to speed up deletion.
  • Test before deleting: Run find() first to confirm matched documents.
  • Set write concern for critical ops: Ensures reliable and consistent writes.
  • Use collation when needed: Enables case-insensitive deletions.
Comment

Explore