DeleteMany
DeleteMany: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 18 of 100
DeleteMany
Foundations & CRUD → Queries & Schema → Aggregation & Scale → Atlas & Projects
Foundations & CRUD · 1 — Documents · ~6 min · MongoDB — CRUD Operations
What is this?
deleteMany removes every document matching the filter. It is powerful for cleanup jobs and dangerous without a tight filter.
Why should you care?
TTL-style cleanup of expired sessions, purging old notifications, or wiping a test tenant’s rows uses deleteMany.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.notifications.insertMany([
{ userId: 1, read: true, msg: "Welcome" },
{ userId: 1, read: true, msg: "Sale" },
{ userId: 1, read: false, msg: "OTP" }
])
db.notifications.deleteMany({ userId: 1, read: true })
db.notifications.find({ userId: 1 })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Both read notifications for user 1 are deleted.
- The unread OTP remains.
- find shows a single document.
- modified bulk delete is intentional here.
Practice next
- Seed the three notifications.
- Run deleteMany and confirm deletedCount 2.
- Verify OTP still exists.
- Delete only msg: /Sale/.
- Count before and after with countDocuments.
Remember
deleteMany removes all matches. Always double-check the filter. Prefer ranged filters for cleanup jobs.
Clear read inbox older than 30 days
A nightly job deletes read notifications past a date for all users in batches.
Outcome: Storage stays flat without deleting unread OTPs.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!