MongoDB Delete Collection Using db.collection.drop()
MongoDB Delete Collection means removing an entire MongoDB Collection from a selected database. To delete a collection in MongoDB shell or mongosh, select the correct database first and then run db.collection.drop() on the collection that has to be removed.
This is different from deleting documents inside a collection. The drop() method removes the collection itself, along with its indexes. If you only want to remove selected records, use delete operations such as deleteOne() or deleteMany() instead of dropping the collection.
MongoDB delete collection syntax
To delete a MongoDB Collection, use db.collection.drop() command. Following is a step by step guide :
Step 1: Select the database where your collection is, with USE command.
use <database_name>
Step 2: Verify if the collection is present.
show collections
Step 3: Issue drop() command on the collection. Syntax of drop() command is provided below :
db.<collection_name>.drop()
where <collection_name> is mandatory and is the name of MongoDB Collection to be deleted.
Step 4: If the Collection is deleted successfully then ‘true‘ is echoed back as acknowledgement, else ‘false’ would be echoed back.
Before running MongoDB drop collection command
Dropping a MongoDB collection is a database-level change. Before you run the command on a real database, check the following points.
- Confirm that you are connected to the correct MongoDB deployment.
- Run
dbor use the prompt to verify the current database name. - Run
show collectionsto confirm the exact collection name. - Take a backup or export the data if the collection may be needed later.
- Use delete operations instead of
drop()when the collection structure and indexes must remain.
Example 1 – Delete MongoDB Collection
Following is an example to delete MongoDB Collection called customers from MongoDB Database named tutorialkart .
Open Mongo Shell and follow the commands in sequence.
> use tutorialkart
switched to db tutorialkart
> show collections
customers
myNewCollection
> db.customers.drop()
true
> show collections
myNewCollection
>
Following is the explanation for each mongodb command we executed above
- use tutorialkart switched to tutorialkart database.
- show collections listed the collections in the selected database.
- db.customers.drop() deleted (dropped) collection named customers .
- show collections verifies that there is no more a collection called customers .
Example 2 – Delete Collection [Negative Scenario]
In this example, we will try to delete a non-existing collection. The drop() method should return false when the collection does not exist in the current database.
> db.noCollection.drop()
false
MongoDB acknowledges with a false, informing that dropping collection named noCollection is failure.
Delete all documents without dropping the MongoDB collection
If your requirement is to empty a collection but keep the collection name and indexes, do not use drop(). Use deleteMany({}) to remove all documents from the collection.
db.customers.deleteMany({})
The command above removes every document from the customers collection, but the collection remains available. This is useful when application code still expects the collection to exist.
Delete one document, many documents, or the whole MongoDB collection
Choose the MongoDB delete operation based on what you want to remove.
| Requirement | MongoDB command | What is removed? |
|---|---|---|
| Delete one matching document | db.collection.deleteOne(filter) | The first document that matches the filter |
| Delete many matching documents | db.collection.deleteMany(filter) | All documents that match the filter |
| Empty the collection | db.collection.deleteMany({}) | All documents, while keeping the collection |
| Delete the collection | db.collection.drop() | The collection and its indexes |
For reference, MongoDB documents the db.collection.drop() method and the db.collection.deleteMany() method separately because they solve different deletion requirements.
MongoDB drop collection with a collection name stored in a variable
If a collection name is stored in a variable, use bracket notation. This is also helpful when the collection name contains characters that cannot be used safely with dot notation.
const collectionName = "customers";
db[collectionName].drop();
Both db.customers.drop() and db["customers"].drop() refer to the same collection. Bracket notation is safer when the collection name is dynamic.
Common mistakes while deleting a MongoDB collection
- Running drop in the wrong database: Always use
use database_namebefore running the command. - Confusing collection and database deletion:
db.collection.drop()deletes a collection, not the whole database. - Using drop when only records should be deleted: Use
deleteMany()when you want to preserve the collection and indexes. - Ignoring the return value:
truemeans the collection was dropped.falseusually means the collection was not present. - Assuming the command can be undone: Restore the collection from a backup if it was dropped accidentally.
QA checklist for this MongoDB delete collection tutorial
- Does the tutorial clearly state that
drop()deletes the collection, not just documents? - Does the example select the database before running
db.customers.drop()? - Does the negative scenario describe a missing collection, not a missing database?
- Does the tutorial explain the difference between
drop()anddeleteMany({})? - Are all newly added code blocks using PrismJS-compatible language classes?
FAQs on MongoDB delete collection
How do I delete a collection in MongoDB?
Select the database using use database_name, verify the collection with show collections, and run db.collection_name.drop(). MongoDB returns true when the collection is dropped successfully.
What does db.collection.drop() return in MongoDB?
The drop() method returns true when MongoDB drops the collection. It returns false when the collection does not exist in the selected database.
Does MongoDB drop collection delete indexes too?
Yes. Dropping a collection removes the collection data and the indexes defined on that collection. If you only want to remove documents and keep indexes, use deleteMany({}).
How do I delete all documents but keep the MongoDB collection?
Use db.collection_name.deleteMany({}). This removes all documents from the collection but keeps the collection itself available for future inserts.
Is db.collection.remove() the same as db.collection.drop()?
No. drop() removes the entire collection. Older examples may show remove() for deleting documents, but current MongoDB tutorials commonly use deleteOne() and deleteMany() for document deletion.
Conclusion
In this MongoDB Tutorial – MongoDB Delete Collection, we have learnt to delete MongoDB Collection with examples. Use db.collection.drop() only when the full collection has to be removed. Use deleteOne() or deleteMany() when only documents inside the collection have to be deleted.
TutorialKart.com