db.dropDatabase()

Definition

  • db.dropDatabase()
  • 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".

  1. { 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 concernresults in fewer member acknowledgements than write concern"majority", the operation uses"majority". Otherwise, the specified writeconcern is used.

When issued on a sharded cluster, MongoDB converts the specifiedwrite concern to"majority".

See also Behavior.

New in version 4.2.

Behavior

The db.dropDatabase() wraps the dropDatabasecommand.

Locks

Changed in version 3.6.

The operation takes a database lock.

User Management

Changed in version 2.6: This command does not delete theusers associated with the currentdatabase. To drop the associated users, run thedropAllUsersFromDatabase command in the database you aredeleting.

Replica Set and Sharded Clusters

  • Replica Sets
  • At minimum, db.dropDatabase() waits until all collectiondrops in the database have propagated to a majority of the replica setmembers (i.e. uses the write concern "majority").

Starting in MongoDB 4.2, you can specify a write concern to themethod. If you specify a write concern that requires acknowledgementfrom fewer than the majority, the method uses write concern"majority".

If you specify a write concern that requires acknowledgement frommore than the majority, the method uses the specified write concern.

Warning

  • Starting in MongoDB 4.2:
  • If you drop a database and create a new database with the same name, youmust either:

    • Restart all mongos instances and allmongod shard members (including the secondarymembers);
    • Use the flushRouterConfig command on allmongos instances and all mongodshard members (including the secondary members) before readingor writing to that database.This ensures that the mongos and shard instancesrefresh their metadata cache, including the location of theprimary shard for the new database.

Otherwise, the you may miss data on reads, and may not write data tothe correct shard. To recover, you must manually intervene.

  • In MongoDB 4.0 and earlier:
  • If you drop a database and create a new database with the same name, youmust either restart all mongos instances, or use theflushRouterConfig command on all mongosinstances before reading or writing to that database. Thisensures that the mongos instances refresh theirmetadata cache, including the location of the primary shard for the new database.

Otherwise, the you may miss data on reads, and may not write data tothe correct shard. To recover, you must manually intervene.

Change Streams

The db.dropDatabase() method and dropDatabasecommand create an invalidate Event for anyChange Streams opened on the dropped database or opened on thecollections in the dropped database.

Example

The following example in the mongo shell uses the use<database> operation to switch the current database to the tempdatabase and then uses the db.dropDatabase() method to dropsthe temp database:

  1. use temp
  2. db.dropDatabase()

See also

dropDatabase