Deleting Documents with deleteOne() and deleteMany()

MongoDB provides delete operations for removing documents that match a filter.

deleteOne()

db.users.deleteOne({ email: "arun@example.com" });

This deletes at most one matching document. If several documents match, do not rely on which one is removed unless the application has designed a deterministic selection strategy.

deleteMany()

db.users.deleteMany({ active: false });

This deletes every document matching the filter.

Empty filter

db.users.deleteMany({});

An empty filter matches every document in the collection. This is a destructive operation that can empty the collection.

Inspect before deleting

A safer workflow is to run a read query using the same filter:

db.users.find({ active: false });

Verify the target documents before executing the deletion.

Delete result

An acknowledged delete result commonly contains deletedCount, indicating the number of documents deleted.

A zero count means no document matched the delete operation at execution time. It does not necessarily indicate a database connection failure.

Referential considerations

MongoDB does not automatically cascade-delete arbitrary references in other collections. If an application deletes a user document, related orders, comments, or audit records remain unless the application or a separate workflow handles them.

Design deletion behavior deliberately. Some systems use soft deletion to retain records while hiding them from ordinary application queries.

Soft deletion

A common approach is:

db.users.updateOne(
  { _id: userId },
  {
    $set: {
      deletedAt: new Date(),
      isDeleted: true
    }
  }
);

Soft deletion is an application pattern rather than a universal MongoDB requirement. Queries, unique indexes, retention policies, and privacy requirements must all account for the chosen approach.

Permanent deletion

Permanent deletion should follow the application's retention and authorization requirements. A database filter is not a substitute for verifying that the caller has permission to delete the selected records.

Verify destructive filters

An empty filter passed to deleteMany() can delete every document in the collection. Confirm the target set and environment before executing destructive operations.

Inspect and delete selected documents
db.deleteDemo.insertMany([
  { name: "Arun", active: true },
  { name: "Meera", active: false },
  { name: "Ravi", active: false }
]);

// Inspect documents that would be deleted.
db.deleteDemo.find({ active: false });

// Delete matching documents.
const result = db.deleteDemo.deleteMany({ active: false });

print("Deleted count:", result.deletedCount);

db.deleteDemo.find();
💡 The example first queries the target documents, then deletes those matching the same filter, and finally verifies the remaining data. The deletedCount result reports how many documents were removed.
Key Takeaways
  • deleteOne() removes at most one matching document.
  • deleteMany() removes every matching document.
  • An empty deleteMany() filter matches all documents.
  • MongoDB does not automatically cascade-delete arbitrary referenced documents.
  • Soft deletion is an application pattern that requires consistent query and retention behavior.
Knowledge Check

What does `deleteMany({})` do?