How to delete everything in a MongoDB database?


You can delete everything in a MongoDB database using dropDatabase() function. The syntax is as follows −

use yourDatabaseName;
db.dropDatabase();

The above syntax will delete everything in a MongoDB database.

To delete everything in a MongoDB database, let us first display all the databases from MongoDB. The query is as follows −

> show dbs

The following is the output −

use yourDatabaseName;
admin 0.000GB
config 0.000GB
flighInformation 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

Now we will delete everything from the database ‘flightInformation’.

First, you need to switch the database to ‘flightInformation’. The query is as follows −

> use flighInformation;
switched to db flighInformation

Now here is the query to delete everything from database ‘flightInformation’ −

> db.dropDatabase();

The following is the output −

{ "dropped" : "flighInformation", "ok" : 1 }

Here is the query to list all databases −

> show dbs;

The following is the output −

admin 0.000GB
config 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

Look at the sample output, everything has been deleted from the database ‘flightInformation’.

Updated on: 30-Jul-2019

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements