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' })`.

CategoryWrite
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.deleteMany(<filter>, { writeConcern: <options> })

⚙️ Parameters

filterPredicate matching documents to delete; empty {} deletes all.
writeConcernOptional. 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏