Navigation

dropDatabase

On this page

Definition

dropDatabase

The dropDatabase command drops the current database, deleting the associated data files.

The command has the following form:

{ dropDatabase: 1, writeConcern: <document>}

The command takes the following optional field:

Field Description
writeConcern

Optional. A document expressing the write concern of the drop command. Omit to use the default write concern.

When issued on a sharded cluster, MongoDB converts the write concern of the dropDatabase command to "majority".

The mongo shell also provides the helper method db.dropDatabase().

Behavior

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

Changed in version 3.6: dropDatabase waits until all collections drops in the database have propagated to a majority of the replica set members.

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 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:

use temp
db.runCommand( { dropDatabase: 1 } )
←   drop dropIndexes  →