Docs Menu
Docs Home
/
MongoDB Manual
/ / /

db.collection.drop()

On this page

  • Definition
  • Compatibility
  • Syntax
  • Behavior
  • Example
db.collection.drop(<options>)

MongoDB with drivers

This page documents a mongosh method. To see the equivalent method in a MongoDB driver, see the corresponding page for your programming language:

C#Java SyncNode.jsPyMongoCC++GoJava RSKotlin CoroutineKotlin SyncPHPMongoidRustScala

Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection. The method provides a wrapper around the drop command.

Returns:true

Note

If the specified collection doesn't exist, db.collection.drop() still returns true.

You can use db.collection.drop() for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The drop() method has the following form:

db.collection.drop( { writeConcern: <document> } )

The drop() method takes an optional document with the following field:

Field
Description
writeConcern

Optional. A document expressing the write concern of the db.collection.drop() operation. Omit to use the default write concern.

When issued on a sharded cluster, mongos converts the write concern of the drop command and its helper db.collection.drop() to "majority".

  • The db.collection.drop() method and drop command create an invalidate for any Change Streams opened on dropped collection.

  • The db.collection.drop() method and drop command abort any in-progress index builds on the target collection before dropping the collection.

    For replica sets or shard replica sets, aborting an index on the primary does not simultaneously abort secondary index builds. MongoDB attempts to abort the in-progress builds for the specified indexes on the primary and if successful creates an associated abort oplog entry. Secondary members with replicated in-progress builds wait for a commit or abort oplog entry from the primary before either committing or aborting the index build.

  • Dropping a collection deletes its associated zone/tag ranges.

  • Starting in MongoDB 5.0, the drop command and the db.collection.drop() method return an error if you try to drop a collection in the admin database or the config database from a mongos. To drop these collections, connect to the config server and run the command there.

    Warning

    Dropping collections in the admin database or the config database can leave your cluster in an unusable state.

  • Starting in MongoDB 6.0, the db.collection.drop() method drops the specified collection and any internal collections related to encrypted fields.

    Warning

    The db.collection.drop() method's behavior differs from the driver's drop method's behavior. The driver's connection must have automatic encryption enabled in order to drop both the specified collection and any internal collections related to encrypted fields. mongosh always drops the specified collection and any internal collections related to encrypted fields.

On a sharded cluster, if you create a collection that has the same name as a previously deleted collection prior to MongoDB 5.0, mongos may forward operations to the wrong shard. To avoid this situation use the version-specific instructions below:

For a sharded cluster running MongoDB 5.0 or later, no special action is required. Use the drop() method and then create a new collection with the same name.

For a sharded cluster, if you use the drop() method and then create a new collection with the same name, you must either:

Flushing the cached routing tables is the preferred procedure because it is faster than removing sharded collections with db.collection.remove(). Only use the remove() approach if you want to avoid flushing the cache.

db.collection.drop() obtains an exclusive lock on the specified collection for the duration of the operation. All subsequent operations on the collection must wait until db.collection.drop() releases the lock.

The following operation drops the students collection in the current database.

db.students.drop()

db.collection.drop() accepts an options document.

The following operation drops the students collection in the current database. The operation uses the 1 write concern:

db.students.drop( { writeConcern: { w: 1 } } )

Back

db.collection.distinct