MongoDB Delete Documents

To delete documents in MongoDB use deleteOne to remove the first matching document and deleteMany to remove every match. The filter works exactly like the one in find(), so test it with find() first.

deleteOne()

Remove a single product by name.

db.products.deleteOne({ name: "Wheat Flour" })
{ acknowledged: true, deletedCount: 1 }

deleteMany()

Remove all products that are out of stock. Honey was restocked in the Update chapter, so nothing matches and deletedCount is 0.

db.products.deleteMany({ stock: 0 })
{ acknowledged: true, deletedCount: 0 }

findOneAndDelete()

This removes a document and returns it, which is handy for job queues.

db.jobs.insertMany([{ task: "email", createdAt: 1 }, { task: "report", createdAt: 2 }])
db.jobs.findOneAndDelete({}, { sort: { createdAt: 1 } })
{ _id: ObjectId('6720a8...'), task: 'email', createdAt: 1 }

Dropping Collections and Databases

deleteMany({}) empties a collection but keeps it and its indexes. drop() removes the collection completely, and dropDatabase() removes the current database.

db.jobs.drop()
db.dropDatabase()
true
{ ok: 1, dropped: 'shop' }

Safety Tips

  • Run find() with the same filter before deleting
  • An empty filter {} matches every document
  • Use soft deletes (a deletedAt field) when data might need restoring
  • Atlas backups and point-in-time restore protect production data

Frequently asked questions

How do I delete all documents in a MongoDB collection?

Use db.collection.deleteMany({}). To remove the collection and its indexes entirely, use db.collection.drop(), which is faster for large collections.

Can I undo a delete in MongoDB?

Not directly. You need a backup, Atlas point-in-time recovery, or to have deleted inside a transaction that you abort.

Is remove() still available?

remove() is deprecated. Use deleteOne or deleteMany instead.