Easy Tutorial
❮ Mongodb Remove Mongodb Database References ❯

MongoDB Deleting a Database

Syntax

The syntax for deleting a database in MongoDB is as follows:

db.dropDatabase()

This command deletes the current database, which defaults to "test". You can use the db command to view the current database name.

Example

In the following example, we delete the database "tutorialpro".

First, view all databases:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
tutorialpro  0.000GB

Next, switch to the "tutorialpro" database:

> use tutorialpro
switched to db tutorialpro
>

Execute the delete command:

> db.dropDatabase()
{ "dropped" : "tutorialpro", "ok" : 1 }

Finally, check if the database was deleted successfully with the show dbs command:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Deleting a Collection

The syntax for deleting a collection is as follows:

db.collection.drop()

The following example deletes the collection "site" from the "tutorialpro" database:

> use tutorialpro
switched to db tutorialpro
> db.createCollection("tutorialpro")     # Create a collection, similar to a table in a database
> show tables             # The `show collections` command would be more accurate
tutorialpro
> db.tutorialpro.drop()
true
> show tables
>
❮ Mongodb Remove Mongodb Database References ❯