How to drop a collection in MongoDB
Delete a MongoDB collection with drop(), drop several at once, and when deleteMany({}) is better. Includes our drop vs deleteMany timing on 200k docs.
Quick Answer
Call drop() on the collection:
use shop
db.orders.drop() // true
The collection, its documents, its indexes and its validation rules are gone. There's no undo.
Drop a collection in mongosh
db.<name>.drop() works when the name is a plain identifier. For names with dashes, spaces or a leading digit, use getCollection():
db.orders.drop()
db.getCollection('order-items').drop()
db['2024 archive'].drop()
drop() returns true when it succeeds. On MongoDB 7.0 it also returns true when the collection doesn't exist (Node.js returns true too, PyMongo returns None), so a typo won't raise an error. Check the name with show collections first.
Dropping the last collection in a database usually removes the database too, but not always. See the findings below.
drop() or deleteMany({})?
Both leave you with no documents, but they're not the same:
drop() | deleteMany({}) | |
|---|---|---|
| Documents | Removed | Removed |
| Indexes | Removed | Kept |
| Validator, collation and other options | Removed | Kept |
| Speed on 200k documents (our test) | 8 ms | 1,594 ms, plus one oplog entry per document on a replica set |
| Privilege needed | dropCollection | remove |
Use drop() when you're done with the collection, or when you'll rebuild it from scratch (recreating indexes on an empty collection is cheap). Use deleteMany({}) when other code depends on the indexes and validation rules staying in place.
What we found testing this
1. drop() was 200ร faster than deleteMany({}) on 200,000 documents
Setup: 200,000 documents of about 150 bytes each, two secondary indexes, a standalone MongoDB 7.0.45 in Docker on an Apple-silicon Mac.
| Command | Time | Left behind |
|---|---|---|
deleteMany({}) | 1,594 ms | Empty collection with its 3 indexes |
drop() | 8 ms | Nothing |
drop() removes the underlying files; deleteMany deletes and un-indexes every document one by one. On a replica set the gap is wider still, because deleteMany also writes one oplog entry per document that every secondary has to replay.
2. Dropping the last collection doesn't always remove the database
If the database ever held a view or a time series collection, MongoDB created a hidden system.views collection there. Drop everything else and it stays, keeping the database in show dbs:
db.cpu.drop() // the last time series collection
db.getCollectionNames() // [ 'system.views' ]
Use db.dropDatabase() if you want it gone.
3. Dropping a time series collection also drops its buckets
A time series collection cpu is stored in system.buckets.cpu. db.cpu.drop() removes both, so there's no separate cleanup step.
4. Views over a dropped collection fail silently
After we dropped the source of a view, the view was still listed, and querying it returned [] with no error. If dashboards suddenly show nothing, check whether a view's source collection was dropped or renamed.
Drop several collections at once
Filter the collection names, then drop each one. Print the list first and run the drop only once it looks right:
// preview
db.getCollectionNames().filter(n => n.startsWith('log_'))
// then drop
db.getCollectionNames()
.filter(n => n.startsWith('log_'))
.forEach(n => db.getCollection(n).drop())
To remove every collection in a database, it's simpler to drop the database.
From Node.js
import { MongoClient } from 'mongodb'
const client = new MongoClient(process.env.MONGODB_URI)
await client.connect()
await client.db('shop').collection('orders').drop() // resolves to true
// or: await client.db('shop').dropCollection('orders')
await client.close()
From Python (PyMongo)
from pymongo import MongoClient
db = MongoClient("mongodb://localhost:27017")["shop"]
db["orders"].drop()
# or: db.drop_collection("orders")
print(db.list_collection_names())
Views, permissions and replica sets
- Views are dropped the same way, with
db.myView.drop(). Dropping a view leaves its source collection untouched. - Permissions:
readWriteincludesdropCollection, so any app user with write access can drop collections. Give read-only tools thereadrole. - Replica sets: run it on the primary. The drop replicates to the secondaries like any other write.
Frequently Asked Questions
Can I recover a dropped collection?
Only from a backup, such as a mongodump archive or an Atlas snapshot. MongoDB has no recycle bin.
Does drop() remove indexes?
Yes. Indexes, validation rules and collection options are deleted with the collection. Use deleteMany({}) if you want to keep them.
Why does drop() return true for a collection that does not exist?
MongoDB 7.0 treats dropping a missing collection as success, and mongosh and the Node.js driver return true. Older servers returned false.
How do I empty a collection without dropping it?
Run db.collection.deleteMany({}). It removes every document but keeps the collection, its indexes and its validator.