Chapter 9: MongoDB mongosh Delete
MongoDB mongosh Delete — the D in CRUD.
This is how you permanently remove documents from a collection. Once deleted, they’re gone (unless you have backups or oplog/time-travel — but that’s advanced).
1. The Two Main Delete Methods in mongosh (2026 Standard — Official & Recommended)
| Method | What it does | How many documents deleted? | Typical return value | Best for |
|---|---|---|---|---|
| db.collection.deleteOne(filter, options) | Deletes the first document that matches the filter | At most 1 | { acknowledged: true, deletedCount: 0 or 1 } | Precise removal: delete one specific wish, one user by _id, one broken record |
| db.collection.deleteMany(filter, options) | Deletes all documents that match the filter | 0 to many | { acknowledged: true, deletedCount: N } | Bulk cleanup: remove all old wishes, delete all from one sender, clear expired entries |
Very important teacher warnings:
- There is no “undo” button — be very careful with filters (especially empty {}).
- The old db.collection.remove() method is deprecated since ~2015–2016 and gives warnings in mongosh. → Always use deleteOne() or deleteMany() in 2026.
- If no documents match the filter → deletedCount: 0 (silent success, no error).
- Deletion is permanent on that node — but in replica sets, it replicates to secondaries.
2. Hands-on Examples — Using Our valentines2026 Database
Assume we still have db.wishes with these documents (from earlier lessons):
|
0 1 2 3 4 5 6 7 8 9 10 11 |
[ { "_id": ObjectId("...a"), "from": "Rahul", "to": "Priya", "message": "Happy Valentine's...", "rating": 10, "likes": 5 }, { "_id": ObjectId("...b"), "from": "Amit", "to": "Sneha", "message": "...", "likes": 3 }, { "_id": ObjectId("...c"), "from": "Priya", "to": "Rahul", "message": "...", "rating": 11, "likes": 7 }, { "_id": ObjectId("...d"), "from": "Rahul", "to": "Everyone", "message": "Group love!", "likes": 2 } ] |
First, always good practice: preview what you’ll delete with find().
|
0 1 2 3 4 5 6 7 |
// Preview: see what matches before deleting db.wishes.find({ from: "Rahul" }).pretty() |
Example 1: deleteOne() — Remove one specific document
|
0 1 2 3 4 5 6 |
db.wishes.deleteOne({ from: "Rahul", to: "Priya" }) |
Output example:
|
0 1 2 3 4 5 6 |
{ acknowledged: true, deletedCount: 1 } |
→ Only the first matching wish (Rahul → Priya) is gone. If multiple Rahul → Priya existed, only one would be deleted.
Safer way (using unique _id):
|
0 1 2 3 4 5 6 |
db.wishes.deleteOne({ _id: ObjectId("67abcdf1234567890fedcba9") }) |
→ Always safest when you know the exact _id.
Example 2: deleteMany() — Bulk delete (very powerful, be careful!)
|
0 1 2 3 4 5 6 7 |
// Delete ALL wishes from "Rahul" db.wishes.deleteMany({ from: "Rahul" }) |
Output example:
|
0 1 2 3 4 5 6 |
{ acknowledged: true, deletedCount: 2 } |
→ Both Rahul wishes gone (the one to Priya and the group one).
Example 3: Delete everything (Danger zone — use with caution!)
|
0 1 2 3 4 5 6 7 |
// Delete ALL documents in the collection db.wishes.deleteMany({}) |
Output:
|
0 1 2 3 4 5 6 |
{ acknowledged: true, deletedCount: 4 } // or whatever was left |
→ Collection is now empty (but still exists). Never run this accidentally in production!
Example 4: Delete with comparison / complex filter
|
0 1 2 3 4 5 6 7 8 9 10 |
// Delete low-rated wishes (rating < 10) db.wishes.deleteMany({ rating: { $lt: 10 } }) // Delete wishes older than Valentine's Day (if you have date field) db.wishes.deleteMany({ date: { $lt: ISODate("2026-02-14T00:00:00Z") } }) |
Example 5: Delete one and return the deleted document (findOneAndDelete)
Sometimes you want to see what was deleted:
|
0 1 2 3 4 5 6 |
db.wishes.findOneAndDelete({ from: "Amit" }) |
Output: Returns the deleted document itself (or null if none found).
|
0 1 2 3 4 5 6 7 8 9 10 11 |
{ "_id": ObjectId("...b"), "from": "Amit", "to": "Sneha", ... } |
→ Useful in workflows where you need the old data before removal.
3. Quick Cheat Sheet Table
| Goal | Method | Filter example | Typical Command | Safety Tip |
|---|---|---|---|---|
| Delete one specific document | deleteOne | { _id: ObjectId(“…”) } | deleteOne({ from: “Rahul”, to: “Priya” }) | Use _id when possible |
| Delete all from one sender | deleteMany | { from: “Rahul” } | deleteMany({ from: “Rahul” }) | Preview with find() first |
| Delete low-quality entries | deleteMany | { rating: { $lt: 6 } } | deleteMany({ rating: { $lt: 6 } }) | — |
| Clear entire collection (careful!) | deleteMany | {} | deleteMany({}) | Think twice — irreversible |
| Delete & see what was removed | findOneAndDelete | { email: “old@ex.com” } | findOneAndDelete({ … }) | Returns the doc |
| Delete with write concern (safer) | — | — | Add { writeConcern: { w: “majority” } } | Waits for replica confirmation |
4. Mini Exercise — Try Right Now!
- use valentines2026
- Preview: db.wishes.find({ from: “Rahul” }).pretty()
- Delete one Rahul wish: db.wishes.deleteOne({ from: “Rahul” })
- Delete the rest from Rahul: db.wishes.deleteMany({ from: “Rahul” })
- Check remaining: db.wishes.find().pretty()
- (Optional — careful) Clear all: db.wishes.deleteMany({}) then show collections (collection still exists, just empty)
Understood beta? Deleting is powerful but emotional — always preview with find() first, like checking if you’re really ready to let go.
Next class options:
- Drop collection or drop database (when you want to remove everything permanently)?
- findOneAndDelete vs deleteOne deep dive?
- Combining all CRUD in a small Valentine Message Board script?
- Indexing so finds/deletes are fast?
- Or move to Node.js/Python connection with real code?
Tell me what your heart desires next — class is continuing! 🚀❤️
Any doubt about delete? Ask freely — no broken hearts here! 😄
