Navigation

db.collection.drop()

On this page

Definition

db.collection.drop(<options>)

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.

db.collection.drop() has the form:

Changed in version 4.0: db.collection.drop() accepts an options document.

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

db.collection.drop() 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".

New in version 4.0.

Returns:
  • true when successfully drops a collection.
  • false when collection to drop does not exist.

Behavior

Resource Locking

Changed in version 4.2.

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.

Prior to MongoDB 4.2, db.collection.drop() obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.

Example

Drop a Collection Using Default Write Concern

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

db.students.drop()

Drop a Collection Using w: "majority" Write Concern

Changed in version 4.0: db.collection.drop() accepts an options document.

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

db.students.drop( { writeConcern: { w: "majority" } } )