Docs Home → Develop Applications → MongoDB Manual
db.dropDatabase()![](/docs/v5.3/assets/link.svg)
On this page
Definition![](/docs/v5.3/assets/link.svg)
db.dropDatabase(<writeConcern>)
Removes the current database, deleting the associated data files.
The
db.dropDatabase()
method takes an optional parameter:FieldDescriptionwriteConcernOptional. A document expressing the write concern to use if greater than
"majority"
.{ w: <value>, j: <boolean>, wtimeout: <number> } Omit to use the default/minimum write concern of
"majority"
.When issued on a replica set, if the specified write concern results in fewer member acknowledgements than write concern
"majority"
, the operation uses"majority"
. Otherwise, the specified write concern is used.When issued on a sharded cluster, MongoDB converts the specified write concern to
"majority"
.See also Behavior.
New in version 4.2.
Behavior![](/docs/v5.3/assets/link.svg)
The db.dropDatabase()
wraps the dropDatabase
command.
Locks![](/docs/v5.3/assets/link.svg)
Starting in version 4.2.2, the operation takes an exclusive (X) database lock only.
In versions 3.6-4.2.1, the operation takes an exclusive (X) database lock while dropping the collections in the database but a global lock when dropping the now-empty database.
User Management![](/docs/v5.3/assets/link.svg)
This command does not delete the
users associated with the current
database. To drop the associated users, run the
dropAllUsersFromDatabase
command in the database you are
deleting.
Indexes![](/docs/v5.3/assets/link.svg)
Starting in MongoDB 4.4, the db.dropDatabase()
method and
dropDatabase
command abort any in-progress index builds
on collections in the target database before dropping the database.
Aborting an index build has the same effect as dropping the built
index. Prior to MongoDB 4.4, attempting to drop a database that
contains a collection with an in-progress index build results in an
error, and the database 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.
Replica Set and Sharded Clusters![](/docs/v5.3/assets/link.svg)
- Replica Sets
At minimum,
db.dropDatabase()
waits until all collection drops in the database have propagated to a majority of the replica set members (i.e. uses the write concern"majority"
).Starting in MongoDB 4.2, you can specify a write concern to the method. If you specify a write concern that requires acknowledgement from fewer than the majority, the method uses write concern
"majority"
.If you specify a write concern that requires acknowledgement from more than the majority, the method uses the specified write concern.
- Sharded Clusters
When issued on a sharded cluster, MongoDB converts the specified write concern to
"majority"
.If you intend to create a new database with the same name as the dropped database, you must follow these additional steps for using the
dropDatabase
command, specific to your version of MongoDB:For MongoDB 4.4 and later, you must:
Run the
dropDatabase
command on amongos
.Once the command successfully completes, run the
dropDatabase
command once more on amongos
.
For MongoDB 4.2, you must:
Run the
dropDatabase
command on amongos
.Once the command successfully completes, run the
dropDatabase
command once more on amongos
.Use the
flushRouterConfig
command on allmongos
instances before reading or writing to that database.
For MongoDB 4.0 and earlier, you must:
Run the
dropDatabase
command on amongos
.Connect to each shard's primary and verify that the namespace has been dropped. If it has not, rerun the
dropDatabase
command again directly from the primary.Connect to a
mongos
, switch to the config database, and remove any reference to the removed namespace from thedatabases
,collections
,chunks
,tags
, andlocks
collections:use config db.collections.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.databases.remove( { _id: "DATABASE" }, {writeConcern: {w: 'majority' }} ) db.chunks.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.tags.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) db.locks.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) Where
DATABASE
represents the namespace of the database you just dropped.Connect to the primary of each shard, and remove any reference to the removed namespace from the
cache.databases
,cache.collections
, andcache.chunks.DATABASE.COLLECTION
collections:db.getSiblingDB("config").cache.databases.remove({_id:"DATABASE"}, {writeConcern: {w: 'majority' }}); db.getSiblingDB("config").cache.collections.remove({_id:/^DATABASE.*/}, {writeConcern: {w: 'majority' }}); db.getSiblingDB("config").getCollectionNames().forEach(function(y) { if(y.indexOf("cache.chunks.DATABASE.") == 0) db.getSiblingDB("config").getCollection(y).drop() }) Where
DATABASE
represents the namespace of the database you just dropped.Use the
flushRouterConfig
command on allmongos
instances before reading or writing to that database.
These steps ensure that all cluster nodes refresh their metadata cache, which includes the location of the primary shard for the new database. Otherwise, you may miss data on reads, and may not write data to the correct shard. To recover, you must manually intervene.
Starting in MongoDB 5.0, the
dropDatabase
command and thedb.dropDatabase()
method return an error if you try to drop the admin database or the config database from amongos
.Warning
Dropping the admin database or the config database can leave your cluster in an unusable state.
Change Streams![](/docs/v5.3/assets/link.svg)
The db.dropDatabase()
method and dropDatabase
command create an invalidate
Event for any
Change Streams opened on the dropped database or opened on the
collections in the dropped database.
Example![](/docs/v5.3/assets/link.svg)
The following example in mongosh
uses the use
<database>
operation to switch the current database to the temp
database and then uses the db.dropDatabase()
method to drops
the temp
database:
use temp db.dropDatabase()