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 beginnersbook 0.000GB local 0.000GB
It is showing two default databases and one database “beginnersbook” 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 “beginnersbook”.
> use beginnersbook switched to db beginnersbook
Note: Change the database name in the above command, from
beginnersbook
to the database that needs to be deleted.
3. Now, the currently selected database is beginnersbook
so the command db.dropDatabase()
would delete this database.
> db.dropDatabase() { "dropped" : "beginnersbook", "ok" : 1 }
The command executed successfully and showing the operation “dropped” and status “ok” which means that the database is dropped.
4. To verify that the database is deleted successfully. Execute the show dbs command again to see the list of databases after deletion.
As you can see that the database “beginnersbook” is not present in the list that means it has been dropped successfully.
Leave a Reply