deleteOne() and deleteMany()
db.collection.deleteOne(filter) removes the first document matching filter, while deleteMany(filter) removes every matching document. Both return a result with deletedCount, and both operations are permanent: MongoDB does not offer a built-in trash bin or undo, so a mistyped or overly broad filter can delete far more data than intended in a single call.
Cricket analogy: It is like a scorer permanently striking out one specific no-ball entry versus wiping every recorded no-ball from an entire tournament's records — deleteOne() removes a single row, deleteMany() sweeps every match.
Deletion Filters and Safety Practices
Because deleteMany({}) with an empty filter removes every document in the collection, it is good practice to first run the equivalent find(filter) or countDocuments(filter) to confirm exactly which and how many documents a deletion will target before switching the call to deleteMany(). Deletion filters support the same query operators as find(), including $lt for expiring old records or $in for removing a specific known set of documents.
Cricket analogy: It is like a groundskeeper double-checking exactly which seats are marked for demolition before knocking any of them out, rather than trusting a vague instruction that could level the wrong stand entirely.
// Preview before deleting: count how many documents would be removed
db.sessions.countDocuments({ expiresAt: { $lt: new Date() } });
// Delete a single document by its unique _id
db.products.deleteOne({ _id: ObjectId("64f1a2b3c4d5e6f7a8b9c0d1") });
// Delete every expired session document
db.sessions.deleteMany({ expiresAt: { $lt: new Date() } });
// Drop the entire collection, including its indexes
db.tempImports.drop();deleteMany({}) removes all documents but keeps the collection, its indexes, and its validators intact — it just becomes empty. db.collection.drop() is different: it removes the collection itself, including all of its indexes, which must then be recreated if the collection is used again.
TTL Indexes for Automatic Expiration
For data that should expire automatically, such as session tokens or verification codes, a TTL (time-to-live) index on a Date field lets MongoDB delete documents itself once they age past a configured number of seconds, via a background task that runs roughly every 60 seconds. This avoids writing and scheduling manual deleteMany() cleanup jobs for time-based expiry.
Cricket analogy: It is like a stadium automatically voiding unused match-day tickets exactly 24 hours after the final ball is bowled, without a staff member manually checking and revoking each one — a TTL index automates that expiry.
There is no rollback for deleteOne() or deleteMany() once acknowledged — always test destructive filters with find() or countDocuments() first, especially in production, and consider taking a backup or snapshot before running a broad deleteMany() against live data.
- deleteOne() removes the first matching document; deleteMany() removes every matching document.
- Both return a result with deletedCount and are permanent — there is no built-in undo.
- deleteMany({}) empties a collection but keeps it and its indexes intact.
- db.collection.drop() removes the entire collection along with its indexes.
- Deletion filters support the same query operators as find(), like $lt and $in.
- Always preview a deletion filter with find() or countDocuments() before running it as a delete.
- TTL indexes let MongoDB automatically expire and delete documents based on a Date field.
Practice what you learned
1. What does db.sessions.deleteMany({}) do?
2. How does db.collection.drop() differ from deleteMany({})?
3. What is a recommended safety practice before running a broad deleteMany() in production?
4. What does a TTL index do?
5. Roughly how often does the TTL background cleanup task run?
Was this page helpful?
You May Also Like
Updating Documents
Learn how to modify existing documents using updateOne, updateMany, replaceOne, update operators like $set and $inc, and upserts.
Querying Documents
Learn how to retrieve documents from MongoDB collections using find() and findOne(), filters, projections, and cursor methods.
Inserting Documents
Learn how to add data to MongoDB collections with insertOne and insertMany, how _id is generated, and how schema flexibility affects inserts.