Definition
db.collection.drop(<options>)MongoDB with drivers
This page documents a
mongoshmethod. To see the equivalent method in a MongoDB driver, see the corresponding page for your programming language:Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection.
drop()is a wrapper around thedropcommand.Returns: true
Note
If the specified collection does not exist, db.collection.drop()
still returns true.
Compatibility
This method is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
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 When issued on a sharded cluster, |
Behavior
The
drop()method anddropcommand create an invalidate event for any change streams opened on the dropped collection.The
db.collection.drop()method anddropcommand 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
abortoplog 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
dropcommand and thedb.collection.drop()method return an error if you try to drop a collection in the admin database or the config database from amongos. 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,
drop()drops the specified collection and any internal collections related to encrypted fields.Important
The
mongoshdrop()method's behavior differs from the driver'sdropmethod's behavior. The driver's connection must have automatic encryption enabled to drop both the specified collection and any internal collections related to encrypted fields.mongoshalways drops the specified collection and any internal collections related to encrypted fields.
Reusing Dropped Collection Names on Sharded Clusters
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.
Resource Locking
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.
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: 1 Write Concern
The following operation drops the students collection in the
current database. The operation uses the
1 write concern:
db.students.drop( { writeConcern: { w: 1, j: true } } )