How to clear a MongoDB database?

To clear a MongoDB database completely, use the dropDatabase() method. This permanently removes the database and all its collections, documents, and indexes.

Syntax

use yourDatabaseName;
db.dropDatabase();

Example: Clear a MongoDB Database

First, let us show all existing databases ?

show dbs
MyDB    0.000GB
admin   0.000GB
config  0.000GB
local   0.000GB
onlinecustomertracker 0.000GB
test    0.006GB

Now, switch to the database you want to delete and drop it ?

use onlinecustomertracker
db.dropDatabase();
switched to db onlinecustomertracker
{ "dropped" : "onlinecustomertracker", "ok" : 1 }

Verify Result

Check that the database has been removed ?

show dbs
MyDB    0.000GB
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.006GB

Key Points

  • dropDatabase() is irreversible - all data is permanently lost.
  • You must first switch to the target database using use databaseName.
  • The operation returns {"ok": 1} when successful.

Conclusion

Use db.dropDatabase() to completely clear a MongoDB database. Always verify the database name before dropping, as this operation cannot be undone.

Updated on: 2026-03-15T03:25:30+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements