MongoDB Drop Collection
Dropping a collection removes it entirely: its documents, its indexes and its settings. It is the right tool for deleting temporary or obsolete collections, and the fastest way to empty a collection when you do not need to keep its indexes.
This tutorial shows how to use drop(), explains exactly what it removes, compares it with deleteMany({}), shows how to drop only indexes, and covers Java and safety practices.
1. The drop() Method
db.<collection>.drop(<options>)
// options (optional): { writeConcern: { w: "majority" } }
The method returns true when the collection existed and was dropped, and false when there was nothing to drop.
2. Example
use bookstore
db.import_staging.insertMany([{ row: 1 }, { row: 2 }, { row: 3 }])
db.import_staging.createIndex({ row: 1 })
show collections
// books
// import_staging
db.import_staging.drop()
// true
db.import_staging.drop()
// false (already gone)
show collections
// books
3. What drop() Removes
- All documents in the collection.
- All indexes, including the
_idindex. - Collection options: validation rules, collation, time series settings, capped size.
- On a replica set, the collection on every member, because the drop is replicated through the oplog.
It does not remove views defined on the collection. Those views remain, but return no documents until the collection is re-created.
drop event followed by an invalidate event, and then close.4. drop() vs deleteMany({})
Both leave you without documents, but they are not the same:
drop() | deleteMany({}) | |
|---|---|---|
| Documents | Removed | Removed |
| Indexes | Removed | Kept |
| Validation rules and options | Removed | Kept |
| Collection itself | Gone | Still exists, empty |
| Speed on large collections | Very fast (metadata change and file removal) | Slow: deletes and logs every document one by one |
| Oplog impact | One entry | One entry per deleted document |
| Can filter which documents | No | Yes, with a query |
To empty a large collection but keep its configuration, a common approach is to drop it and immediately re-create its indexes and options from a script, rather than running deleteMany({}) over millions of documents.
5. Dropping Only Indexes
If you want to keep the data and remove an index, use the index methods instead:
db.books.getIndexes() // list index names
db.books.dropIndex("title_1") // drop one index by name
db.books.dropIndex({ category: 1, price: -1 }) // or by key pattern
db.books.dropIndexes() // drop all indexes except _id
Before dropping an index in production, you can first hide it with db.books.hideIndex("title_1"). The query planner stops using it, but it is still maintained, so you can unhide it instantly if performance suffers.
6. Other Ways to Drop a Collection
The drop command
db.runCommand({ drop: "import_staging", writeConcern: { w: "majority" } })
MongoDB Compass and Atlas
Hover over the collection in the sidebar, click the trash icon, type the collection name to confirm, and choose Drop Collection.
Dropping several collections
// Drop every collection whose name starts with "tmp_"
db.getCollectionNames()
.filter(name => name.startsWith("tmp_"))
.forEach(name => print(name, db.getCollection(name).drop()))
7. Dropping a Collection from Java
import com.mongodb.client.*;
import org.bson.Document;
public class DropCollectionExample {
public static void main(String[] args) {
try (MongoClient client = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase db = client.getDatabase("bookstore");
// Drop one index but keep the data
db.getCollection("books").dropIndex("title_1");
// Drop the whole collection (no error if it does not exist)
db.getCollection("import_staging").drop();
}
}
}
In Spring Data MongoDB, mongoTemplate.dropCollection("import_staging") or mongoTemplate.dropCollection(Book.class) does the same.
8. Permissions and Safety
- Dropping requires the
dropCollectionprivilege, included in roles such asreadWrite,dbAdminanddbOwner. - Because
readWritecan drop collections, give truly read-only consumers thereadrole, and consider custom roles withoutdropCollectionfor sensitive applications. - Check the current database with
dband the collection name before runningdrop(); there is no undo. - Keep tested backups of anything you cannot rebuild.
9. Summary
db.collection.drop()deletes a collection with all its documents, indexes and options, and returnstrueorfalse.drop()is much faster thandeleteMany({})on large collections, butdeleteManykeeps indexes and settings and can filter.- Use
dropIndex()/dropIndexes()to remove indexes only, andhideIndex()to test an index removal safely. - The drop replicates to all members and invalidates change streams on that collection.