Get Started Free

How to drop a database in MongoDB

Delete a MongoDB database with db.dropDatabase() in mongosh, Node.js or Python, what it removes and what it leaves behind, and how to drop it safely.

Updated November 1, 2026 ยท Tested on MongoDB 7.0.45, mongosh 2.9.2, Node.js driver 6.12, PyMongo 4.19

Quick Answer

Switch to the database, then call db.dropDatabase():

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

This deletes every collection, document and index in shop. There's no undo, so check db first.

Check where you are before you drop

dropDatabase() acts on the current database, and the shell doesn't ask for confirmation. A habit that prevents accidents:

db                       // confirm the name
show collections         // confirm it's the one you think
db.dropDatabase()

In scripts, don't rely on the current database at all. Name it explicitly:

db.getSiblingDB('shop_staging').dropDatabase()

Dropping a database that doesn't exist isn't an error, and the result can't tell you it happened. See the findings below for what each client returns.

What gets deleted, and what doesn't

RemovedKept
Every collection and view in the databaseUsers created in that database (they live in admin.system.users)
All documents and indexesCustom roles defined in that database
GridFS files stored in itBackups, snapshots and Atlas cloud backups

Leftover users are easy to miss. After dropping a database for good, remove its users too:

use shop
db.dropAllUsers()     // { n: 2, ok: 1 }
db.dropAllRoles()

What we found testing this

1. A typo looks exactly like success

We dropped a database name that had never existed. Every client reported success, and mongosh even echoes the name back as dropped:

ClientResult for a database that never existed
mongosh{ ok: 1, dropped: 'shop_stagng' }
Node.js drivertrue
PyMongoNone (no exception)

If you need to know a drop really removed something, check first:

const name = 'shop_staging'
if (!db.getMongo().getDBNames().includes(name)) throw new Error(name + ' does not exist')
db.getSiblingDB(name).dropDatabase()

2. Leftover users get their access back

We dropped shop while its readWrite user still existed. The user could still log in, and its next insert recreated shop with the same permissions as before. The list users guide has a snippet that finds these orphaned users.

3. readWrite users get "not authorized", not a friendlier message

The same readWrite user could drop each collection, but not the database:

db.dropDatabase()
// MongoServerError: not authorized on shop to execute command { dropDatabase: 1, ... }

From the terminal

To drop a database in one command, without opening an interactive shell:

mongosh "mongodb://localhost:27017/shop_staging" --eval "db.dropDatabase()"

Putting the database in the connection string, rather than relying on use in the script, makes it obvious which database the command will hit.

From Node.js

import { MongoClient } from 'mongodb'

const client = new MongoClient(process.env.MONGODB_URI)
await client.connect()

await client.db('shop_staging').dropDatabase()   // true, even if it didn't exist

await client.close()

Test suites often do this in a teardown hook. Point them at a dedicated database name (and ideally a dedicated server) so a misconfigured environment variable can't drop production.

From Python (PyMongo)

from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017")
client.drop_database("shop_staging")   # returns None either way

print(client.list_database_names())   # shop_staging is gone

Permissions and errors

  • Dropping a database needs the dropDatabase action on it, which dbAdmin, dbOwner and root include. Plain readWrite can drop collections one by one but not the database.
  • On a replica set, run it against the primary. The drop replicates to the secondaries.
  • Leave the system databases alone. admin and config hold users, roles and cluster metadata, and local holds the replication oplog. A replica set refuses to drop local; a standalone server will let you.

If you only want to empty one collection, use drop a collection instead. To back up first, see mongodump and mongorestore.

Frequently Asked Questions

Can I undo db.dropDatabase()?

No. The only way back is restoring from a backup, such as a mongodump archive or an Atlas snapshot.

Does dropping a database delete its users?

No. Users and roles created in that database are kept in admin.system.users and admin.system.roles. Run db.dropAllUsers() and db.dropAllRoles() in that database to remove them.

How do I drop all databases?

Loop over db.getMongo().getDBNames(), skip admin, config and local, and call db.getSiblingDB(name).dropDatabase() on each. Double-check the server first.

Why is my database back after I dropped it?

Something wrote to it again. Any insert, or an application on startup creating collections, recreates a database automatically.

VisuaLeaf

Manage databases visually in VisuaLeaf

VisuaLeaf lists every database and collection in a sidebar so you always see which one you are in, and its task manager runs and schedules exports and backups before you clean up.