Docs Home → Develop Applications → MongoDB Manual
db.collection.drop()
On this page
Definition
db.collection.drop(<options>)
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
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.Note
For a sharded cluster, if you use
db.collection.drop()
and then create a new collection with the same name, you must either:Flush the cached routing table on every
mongos
usingflushRouterConfig
.Use
db.collection.remove()
to remove the existing documents and reuse the collection. Use this approach to avoid flushing the cache.
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:FieldDescriptionwriteConcernOptional. 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 thedrop
command and its helperdb.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
The
db.collection.drop()
method anddrop
command create aninvalidate
Event for any Change Streams opened on dropped collection.Starting in MongoDB 4.4, the
db.collection.drop()
method anddrop
command abort any in-progress index builds on the target collection before dropping the collection. Prior to MongoDB 4.4, attempting to drop a collection with in-progress index builds results in an error, and the collection is not dropped.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.Starting in MongoDB 4.0.2, 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
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: 1
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
1
write concern:
db.students.drop( { writeConcern: { w: 1 } } )