HOWTO · MongoDB
How to Delete All Records of a Collection in MongoDB Shell
Learn how to delete one or many MongoDB documents, clear a collection while keeping it, and inspect the delete result.
On this page
To clear a MongoDB collection while keeping the collection itself, run db.teams.deleteMany({}) in mongosh, replacing teams with your collection name. The empty filter {} matches every document, so this command removes all documents in that collection. To remove the collection and its indexes as well, use db.teams.drop() instead. Confirm the active database and collection before running either destructive operation.
The examples below use a teams collection. MongoDB deletion methods take a filter document: a field condition narrows which documents match, while an empty filter matches all documents. deleteOne() removes at most one matching document; deleteMany() removes every matching document.
Choose Between Deleting Documents and Dropping a Collection
Choose the operation based on what should remain afterward:
- Use
deleteOne(filter)to remove no more than one document matchingfilter. - Use
deleteMany(filter)to remove all documents matchingfilter. - Use
deleteMany({})to remove every document but keep the collection. - Use
db.teams.drop()to remove the collection itself, including its indexes.
An empty filter is consequential: deleteOne({}) deletes one matching document, but does not promise which document is chosen or that it is first in collection order. deleteMany({}) matches every document. It is not a safe substitute for a selective filter when only some records should be removed.
Delete One Matching Document
Pass a filter to deleteOne() to target a matching document. For example, db.teams.deleteOne({ team: "Arsenal" }) removes at most one document whose team field equals "Arsenal". If several documents match, this method still deletes only one; it does not remove all Arsenal records. Use deleteMany() when every match should be removed.
Calling db.teams.deleteOne({}) also removes at most one document, because the empty filter matches all documents. MongoDB does not guarantee that this is the first document in collection order. To clear the collection, use deleteMany({}) instead.
Delete Every Document in a Collection
Use a condition with deleteMany() when all documents meeting that condition should be removed. For instance, db.teams.deleteMany({ team: "Manchester" }) removes every document whose team field is exactly "Manchester"; it leaves documents for other teams in place. Matching is based on the stored field value, so check spelling and spacing in the data if the deleted count is lower than expected.
To clear the collection but retain it, run db.teams.deleteMany({}). Because {} has no field conditions, every document matches. The collection remains available for later inserts, while its documents are deleted. If the intention is to remove only selected records, do not use this empty-filter form. If the collection itself should no longer exist, use the drop operation described above.
For a small illustration of the filtered operation, this standalone JavaScript example models deleting every document with the team value Manchester from an in-memory list. It does not connect to MongoDB; use the mongosh command above against your database.
const teams = [
{ team: "Manchester", points: 70 },
{ team: "Liverpool", points: 69 },
{ team: "Manchester", points: 50 }
];
const filter = { team: "Manchester" };
const remaining = teams.filter(document => document.team !== filter.team);
console.log(remaining.map(document => document.team).join(", "));
Output:
Liverpool
Check the Delete Result
MongoDB shell deletion methods return a result document that includes deletedCount, the number of documents removed. For example, assign the result and inspect the count with const result = db.teams.deleteMany({ team: "Manchester" }); result.deletedCount. A count of 0 means no documents matched the filter; a positive count tells you how many were deleted. For deleteOne(), the count can be at most 1.
After clearing with deleteMany({}), the result’s deletedCount reports how many documents were removed. If it is 0, the collection may already have been empty, or the command may have targeted a different database or collection than intended. Verify the active database and collection name before retrying. These checks help distinguish a successful empty-collection result from a mistaken target without assuming a particular document order.