00:00

MongoDB Drop Database

Last updated: 4 min read

Dropping a database removes it completely: every collection, every document and every index inside it. It is one of the fastest ways to clean up a test environment, and one of the fastest ways to lose production data by mistake.

This tutorial shows how to drop a database with db.dropDatabase(), what is and is not deleted, how it behaves on replica sets, how to do it from Compass and Java, and the safety habits that prevent accidents.

1. The dropDatabase() Method

db.dropDatabase() drops the current database, the one selected with use:

mongosh
use bookstore_test
db.dropDatabase()
// { ok: 1, dropped: 'bookstore_test' }

The full syntax accepts an optional write concern:

Syntax
db.dropDatabase(<writeConcern>)

// Example: wait until a majority of replica set members have applied the drop
db.dropDatabase({ w: "majority" })

2. Step-by-Step Example

mongosh
// 1. Create a throw-away database with some data
use demo_shop
db.products.insertMany([{ name: "Pen" }, { name: "Notebook" }])
db.customers.insertOne({ name: "Ken" })

show dbs
// ... demo_shop   72.00 KiB ...

// 2. Make sure you are in the right database
db
// demo_shop

// 3. Drop it
db.dropDatabase()
// { ok: 1, dropped: 'demo_shop' }

show dbs
// demo_shop is gone

Dropping a database that does not exist is not an error. MongoDB still answers { ok: 1 }, so the command is safe to repeat in scripts.

3. What Gets Deleted, and What Does Not

RemovedNot removed
All collections and views in the databaseUsers defined in that database
All documentsOther databases
All indexes and collection options (validation rules, collation)Backups you made earlier
The database's data files on every replica set memberRoles granted to users in other databases that mention it

Users are kept so that a re-created database with the same name can be used again immediately. To remove them as well, run:

mongosh
use demo_shop
db.dropAllUsersFromDatabase()

4. Replica Sets and Sharded Clusters

  • On a replica set, run the command on the primary (your connection does this automatically). The drop is recorded in the oplog and applied on every secondary.
  • Use { w: "majority" } when a script immediately re-creates the database or depends on the drop being visible everywhere.
  • On a sharded cluster, connect to mongos and run the same command; MongoDB removes the database from every shard and from the cluster metadata.

5. Required Permissions

The user needs the dropDatabase privilege on that database. Built-in roles that include it are dbAdmin, dbOwner and cluster-wide administrative roles such as dbAdminAnyDatabase and root.

Application users should normally have only readWrite, which does not allow dropping the database. Then a bug or an injected command in the application cannot wipe out the database.

6. Other Ways to Drop a Database

MongoDB Compass

In the database list, hover over the database, click the trash icon, type the database name to confirm, and click Drop Database. Atlas Data Explorer works the same way.

The dropDatabase command

db.dropDatabase() is a shell helper around the dropDatabase database command, which drivers and scripts can call directly:

mongosh
db.getSiblingDB("demo_shop").runCommand({ dropDatabase: 1 })

From the command line

Terminal
mongosh "mongodb://localhost:27017/demo_shop" --quiet --eval 'db.dropDatabase()'

Java driver

DropDatabaseExample.java
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

public class DropDatabaseExample {
    public static void main(String[] args) {
        try (MongoClient client = MongoClients.create("mongodb://localhost:27017")) {
            client.getDatabase("demo_shop").drop();
            System.out.println("Remaining: " + client.listDatabaseNames().into(new java.util.ArrayList<>()));
        }
    }
}

7. Safety Checklist

  1. Check the connection and database first: run db.getMongo() and db to confirm the server and the database.
  2. Have a recent, tested backup of anything you might need again. See MongoDB Backup and Restore.
  3. Use separate users per environment, so a development login simply cannot drop production databases.
  4. Make the shell prompt show the environment, for example with a custom prompt in your .mongoshrc.js.
  5. Prefer dropping individual collections when you only need to clear some data.
  6. On Atlas, consider a Backup Compliance Policy for production projects, so backups cannot be deleted along with the data.

8. Summary

  • db.dropDatabase() permanently removes the current database with all its collections, documents and indexes.
  • Users defined in the database are kept; remove them with db.dropAllUsersFromDatabase().
  • The drop replicates to all replica set members and works on sharded clusters through mongos.
  • Only roles such as dbAdmin, dbOwner or root can drop databases; keep application users on readWrite.
  • Always confirm the target and have a backup before dropping anything that matters.