How to Delete All Documents in MongoDB Quickly and Safely
To delete all documents in a MongoDB collection, use the
deleteMany({}) method with an empty filter object. This command removes every document but keeps the collection itself intact.Syntax
The basic syntax to delete all documents in a MongoDB collection uses the deleteMany() method with an empty filter {}. This means no condition is set, so all documents match and get deleted.
db.collection.deleteMany({}): Deletes all documents in the specified collection.
mongodb
db.collection.deleteMany({})Example
This example shows how to delete all documents from a collection named users. After running the command, the collection remains but is empty.
mongodb
use myDatabase
// Delete all documents from the 'users' collection
db.users.deleteMany({})Output
{ "acknowledged" : true, "deletedCount" : 5 }
Common Pitfalls
One common mistake is using deleteOne({}) which deletes only a single document, not all. Another is accidentally dropping the collection with drop() when you only want to clear documents.
Always double-check your filter. Using {} deletes everything, so be sure this is intended.
mongodb
/* Wrong: deletes only one document */ db.users.deleteOne({}) /* Right: deletes all documents */ db.users.deleteMany({})
Quick Reference
| Command | Description |
|---|---|
| db.collection.deleteMany({}) | Deletes all documents in the collection |
| db.collection.deleteOne({}) | Deletes a single document matching the filter |
| db.collection.drop() | Deletes the entire collection including documents and indexes |
Key Takeaways
Use db.collection.deleteMany({}) to delete all documents but keep the collection.
Avoid deleteOne({}) if you want to remove all documents; it deletes only one.
Do not confuse deleting documents with dropping the collection using drop().
Always confirm your filter is empty ({}) to delete everything safely.
Deleting documents is irreversible; back up data if needed before running.