Drop Database in MongoDB
In the last tutorial we learned how to create a database in MongoDB. Here we will see how to drop a database in MongoDB. We use db.dropDatabase() command to delete a database. You should be very careful when deleting a database as this
will remove all the data inside that database, including collections and documents stored in the database.
MongoDB Drop Database
The syntax to drop a Database is:
db.dropDatabase()
We do not specify any database name in this command, because this command deletes the currently selected database. Lets see the steps to drop a database in MongoDB.
1. See the list of databases using show dbs command.
> show dbs
admin 0.000GB
employeeDB 0.000GB
local 0.000GB
It is showing two default databases and one database “employeeDB” that I have created.
2. Switch to the database that needs to be dropped by typing this command.
use database_name
For example I want to delete the database “employeeDB”.
> use employeeDB
switched to db employeeDB
Note: Change the database name in the above command, from employeeDB to the database that needs to be deleted.
3. Now, the currently selected database is employeeDB so the command db.dropDatabase() would delete this database.
> db.dropDatabase()
{ "dropped" : "employeeDB", "ok" : 1 }
The command executed successfully and showing the operation “dropped” and status “ok” which means that the database is dropped.
> use employeeDB
switched to db employeeDB
> db.dropDatabase()
{ "dropped" : "employeeDB", "ok" : 1 }
>
4. To verify that the database is deleted successfully. Execute the show dbs command again to see the list of databases after deletion.
> use employeeDB
switched to db employeeDB
> db.dropDatabase()
{ "dropped" : "employeeDB", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mean_db 0.000GB
node-demo 0.000GB
userInfo 0.000GB
>
As you can see that the database “employeeDB” is not present in the list that means it has been dropped successfully.