deleteMany() (Delete Multiple Documents)
Last updated: 2026-10-09
deleteMany() (Delete Multiple Documents)
deleteMany() removes every document matching a filter and returns deletedCount. It is the workhorse for bulk cleanup such as purging stale logs or deactivating users. Warning: an empty filter {} empties the whole collection, so verify the filter before running. Deletion is irreversible — for risky operations, first gauge the impact with `db.users.countDocuments({ status: 'expired' })`.
| Category | Write |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.deleteMany(<filter>, { writeConcern: <options> })
⚙️ Parameters
| filter | Predicate matching documents to delete; empty {} deletes all. |
|---|---|
| writeConcern | Optional. Write concern. |
Returns:DeleteResult: { acknowledged, deletedCount }.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// deleteMany: delete every user under 18
db.users.deleteMany({ age: { $lt: 18 } })
Output:
{ acknowledged: true, deletedCount: 5 }
📌 Notes
- An empty filter {} deletes every document in the collection — dangerous
- For large deletions, use indexes to avoid collection scans
- Irreversible; back up or assess with countDocuments first
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat happens if deleteMany gets an empty filter?
AAn empty filter {} matches everything and deletes the entire collection — avoid this by accident.
QWhat is the difference between deleteMany and drop?
AdeleteMany() removes documents but keeps the collection and indexes; drop() removes the collection itself, indexes and metadata included.
QHow do I know how many documents deleteMany removed?
ARead deletedCount from the result: const r = db.users.deleteMany({status: 'del'}); r.deletedCount.