How to clear a MongoDB database?


To clear, use dropDatabase. Following is the syntax −

use yourDatabaseName;
db.dropDatabase();

To clear a MongoDB database, first show all the databases −

> show dbs

This will produce the following output −

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

Now, let us delete the database onlinecustomertracker −

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

Following is the query to show all databases after deleting a database above −

> show dbs

This will produce the following output −

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

Updated on: 13-May-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements