How do I drop a MongoDB database from the command line?

I found this easy to remember:

mongo //to start the mongodb shell

show dbs //to list existing databases

use <dbname> //the <dbname> is the database you'd like to drop

db //should show <dbname> just to be sure I'm working with the right database

db.dropDatabase() //will delete the database & return { "dropped" : "<dbname>", "ok" : 1 }

The best way to do it is from the mongodb console:

> use mydb; 
> db.dropDatabase();

Alternatively, you can stop mongod and delete the data files from your data directory, then restart.

Hint: you can also move the data files to a subfolder, and delete them if you're sure you no longer need them.


Like this:

mongo <dbname> --eval "db.dropDatabase()"

More info on scripting the shell from the command line here: https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#scripting

Tags:

Mongodb