drop
Definition
drop
The
drop
command removes an entire collection from a database.Tip
In
mongosh
, this command can also be run through thedrop()
helper method.Helper methods are convenient for
mongosh
users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
Compatibility
This command 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 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 command has following syntax:
db.runCommand( { drop: <collection_name>, writeConcern: <document>, comment: <any> } )
Command Fields
The command takes the following fields:
Field | Description |
---|---|
drop | The name of the collection to drop. |
writeConcern | Optional. A document expressing the write concern of the When issued on a sharded cluster, |
comment | Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
A comment can be any valid BSON type (string, integer, object, array, etc). |
mongosh
provides the equivalent helper method
db.collection.drop()
.
Behavior
Starting in MongoDB 5.0, the
drop
command and thedb.collection.drop()
method will raise an error if passed an unrecognized parameter.This command also removes any indexes associated with the dropped collection.
The
db.collection.drop()
method anddrop
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.The
drop
command and its helperdb.collection.drop()
create an invalidate for any Change Streams opened on the dropped collection.Dropping a collection deletes its associated zone/tag ranges.
Starting in MongoDB 5.0, the
drop
command 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.
Resource Locking
drop
obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until drop
releases the
lock.