00:00

MongoDB Drop Collection

Last updated: 4 min read

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

Syntax
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

mongosh
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 _id index.
  • 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.

Open change streams on a dropped collection receive a 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({})
DocumentsRemovedRemoved
IndexesRemovedKept
Validation rules and optionsRemovedKept
Collection itselfGoneStill exists, empty
Speed on large collectionsVery fast (metadata change and file removal)Slow: deletes and logs every document one by one
Oplog impactOne entryOne entry per deleted document
Can filter which documentsNoYes, 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:

mongosh
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

mongosh
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

mongosh
// 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

DropCollectionExample.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 dropCollection privilege, included in roles such as readWrite, dbAdmin and dbOwner.
  • Because readWrite can drop collections, give truly read-only consumers the read role, and consider custom roles without dropCollection for sensitive applications.
  • Check the current database with db and the collection name before running drop(); 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 returns true or false.
  • drop() is much faster than deleteMany({}) on large collections, but deleteMany keeps indexes and settings and can filter.
  • Use dropIndex() / dropIndexes() to remove indexes only, and hideIndex() to test an index removal safely.
  • The drop replicates to all members and invalidates change streams on that collection.