Navigation
This is an upcoming (i.e. in progress) version of the manual.

db.dropDatabase()

On this page

Definition

db.dropDatabase(<writeConcern>)

Removes the current database, deleting the associated data files.

The db.dropDatabase() method takes an optional parameter:

Field Description
writeConcern

Optional. 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

The db.dropDatabase() wraps the dropDatabase command.

Locks

Warning

This command obtains a global write lock and will block other operations until it has completed.

User Management

Changed in version 2.6: 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.

Replica Set and Sharded Clusters

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".

Warning

If you drop a database and create a new database with the same name, you must either restart all mongos instances, or use the flushRouterConfig command on all mongos instances before reading or writing to that database. This action ensures that the mongos instances refresh their metadata cache, including the location of the primary shard for the new database. Otherwise, the mongos may miss data on reads and may write data to a wrong shard.

Change Streams

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

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 db.dropDatabase() method to drops the temp database:

use temp
db.dropDatabase()

See also

dropDatabase

←   db.currentOp() db.eval()  →