Labs ICT
Pro Login

Delete Documents

Removing data from your collections.

Deleting Documents in MongoDB

Deleting data is a fact of life in any database. Users leave, products get discontinued, sessions expire—sometimes you need to clean house. MongoDB makes deletion simple with deleteOne() and deleteMany(). But before you start deleting things, let's talk about doing it the right way.

There are two schools of thought when it comes to deletion: hard delete and soft delete. A hard delete physically removes the document from the database. A soft delete marks the document as deleted (like setting an "isActive" field to false) but keeps it in the database. Each approach has its place, and knowing when to use which is important.

Hard deletes are permanent. Once you delete a document, it's gone. No undo button. That's why many developers prefer soft deletes for important data—you can always restore it later if needed. Soft deletes also make it easy to run reports on historical data.

Delete One Document

deleteOne() removes the first document that matches your filter. It's like finding a specific book on a shelf and pulling it off. The method returns an object that tells you how many documents were deleted (0 or 1) and any errors that occurred.

Always use a specific filter with deleteOne(). If you pass an empty filter, MongoDB will delete the first document in the collection, which is probably not what you want. Be intentional about what you're deleting—it's better to be too specific than too broad.

db.users.deleteOne({ name: "Alice Johnson" })

db.sessions.deleteOne({ _id: ObjectId("507f1f77bcf86cd799439011") })
Try it Yourself →

Delete Many Documents

deleteMany() is the big broom. It sweeps out every document that matches your filter. Use it wisely—a bad filter can wipe out your entire collection. Seriously, always test your filter with find() first to make sure you're only deleting what you intend to delete.

This is great for cleanup tasks like removing old session logs, purging expired data, or getting rid of test accounts. Just make sure you've got backups before running any deleteMany() in production. Trust me, you don't want to explain to your boss why the entire users collection is gone.

db.sessions.deleteMany({ expiresAt: { $lt: new Date() } })

db.logs.deleteMany({ level: "debug" })
Try it Yourself →

Soft Delete vs Hard Delete

Soft deletes are like moving something to the trash instead of burning it. You mark the document as deleted by adding a field like "deletedAt" or setting "isActive" to false. The data is still there, just hidden from normal queries. You can restore it easily by removing the flag.

Hard deletes actually remove the document from disk. They free up space and reduce your database size, but there's no going back. Use hard deletes for data you truly don't need anymore—like temporary logs or test data. For anything important, soft delete is usually the safer bet.

db.users.updateOne(
  { name: "Bob Smith" },
  { $set: { isActive: false, deletedAt: new Date() } }
)

db.users.deleteMany({ deletedAt: { $exists: true } })
Try it Yourself →

🧪 Quick Quiz

What does deleteMany() do?