- Reference >
- Database Commands >
- Administration Commands >
- dropDatabase
dropDatabase¶
On this page
Definition¶
-
dropDatabase
¶ The
dropDatabase
command drops the current database, deleting the associated data files.The command has the following form:
The command takes the following optional field:
Field Description writeConcern
Optional. A document expressing the write concern to use if greater than
"majority"
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.
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:
- mongod log messages, in the
attr.command.cursor.comment
field. - Database profiler output, in the
command.comment
field. currentOp
output, in thecommand.comment
field.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
The
mongo
shell also provides the helper methoddb.dropDatabase()
.- mongod log messages, in the
Behavior¶
Locks¶
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¶
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¶
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¶
Changed in version 3.6.
- Replica Sets
At minimum,
dropDatabase
waits until all collections drops in the database have propagated to a majority of the replica set members (i.e. uses the write concern"majority"
).If you specify a write concern that requires acknowledgement from fewer than the majority, the command uses write concern
"majority"
.If you specify a write concern that requires acknowledgement from more than the majority, the command uses the specified write concern.
- Sharded Clusters
When issued on a sharded cluster, MongoDB converts the specified write concern to
"majority"
.For MongoDB 4.2 and previous, when executing the
dropDatabase
command, you must perform the following additional steps if you intend to create a new database with the same name as the dropped database.Note
MongoDB 4.4 does not require these additional steps when dropping and recreating a database with the same name.
- For MongoDB 4.2, you must either:
- Restart all
mongos
instances and allmongod
shard members (including the secondary members); - Use the
flushRouterConfig
command on allmongos
instances and allmongod
shard members (including the secondary members) before reading or writing to that database.
- Restart all
- For MongoDB 4.0 and earlier, you must either:
- Restart all
mongos
instances; - Use the
flushRouterConfig
command on allmongos
instances before reading or writing to that database.
- Restart all
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.
- For MongoDB 4.2, you must either:
Change Streams¶
The db.dropDatabase()
method and dropDatabase
create an invalidate Event for any Change Streams
opened on the dropped database or opened on the collections in the
dropped database.
Example¶
The following example in the mongo
shell uses the use
<database>
operation to switch the current database to the temp
database and then uses the dropDatabase
command to drop
the temp
database:
See also