MongoDB Drop Collection
MongoDB provides the ability to drop collections to clean up collections that are no longer needed. Dropping a collection removes all documents and indexes in the collection.
Drop Collection Methods
Basic Syntax
Example
Check Before Drop
Drop Collection Characteristics
What Gets Dropped
- All documents in the collection
- All indexes of the collection
- The collection itself
What Remains
- The database itself
- Other collections
- Database users and permissions
Performance Characteristics
- Drop operation is very fast (almost instantaneous)
- Doesn't block other operations
- Space is not immediately released (needs compact)
Drop Multiple Collections
Batch Drop
Drop All Collections (Keep Database)
Safe Drop Process
Step 1: Backup Data
Step 2: Confirm Information
Step 3: Execute Drop
Drop vs Empty Comparison
drop() vs deleteMany()
Empty Collection (Keep Structure)
Command Line Drop
Non-Interactive Drop
Script Drop
Common Questions
Q: Space not released after dropping?
A: Use compact command to release space:
Q: How to recover accidentally dropped collection?
A: Restore from backup:
Q: How to drop system collections?
A: Not recommended, but can use:
Summary
Key points for dropping collections:
- Operation is irreversible, backup before dropping
- Drops collection, documents, and indexes simultaneously
- Space is not immediately released
- Faster and more thorough than deleteMany()
In the next chapter, we will learn about MongoDB Insert Document.