MongoDB Delete Database

MongoDB Delete Database – In this MongoDB Tutorial, we shall learn to drop or delete a MongoDB Database.

To delete or drop a database from MongoDB, follow these steps

  1. Select the database you want to delete with the help of USE <database> command. Following is the syntax of USE command.use <database_name>
  2. Drop the database with the help of db.dropDatabase() command. Following is the syntax of USE command.db.dropDatabase()

Example – Drop MongoDB Database

Following is an example where we shall try deleting database named tutorialkart.

Refer MongoDB Create Database, if you have not already created one.

Open Mongo Shell and follow the commands in sequence.

> show dbs
admin         0.000GB
local         0.000GB
tutorialkart  0.000GB
> use tutorialkart
switched to db tutorialkart
> db.dropDatabase()
{ "dropped" : "tutorialkart", "ok" : 1 }
> show dbs
admin  0.000GB
local  0.000GB
>

Following is the explanation for each mongodb command we executed above

  1. show dbs  there are three databases. We shall delete tutorialkart database in this demonstration.
  2. use tutorialkart  switched to tutorialkart database.
  3. db.dropDatabase()  drops the database that is currently in use i.e., tutorialkart database.
  4. show dbs  now there are only two databases, because tutorialkart database is no more present.
ADVERTISEMENT

Conclusion

In this MongoDB Tutorial, we learned how to delete a MongoDB Database.