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

coffee-grinder Source

What's the easiest way to do this from my bash prompt?



answered 7 years ago mnemosyn #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.

answered 7 years ago Tim Gautier #2

Like this:

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

More info on scripting the shell from the command line here:

answered 6 years ago David #3

You could also use a "heredoc":

mongo localhost/db <<EOF

Results in output like:

mongo localhost/db <<EOF
MongoDB shell version: 2.2.2
connecting to: localhost/db
{ "dropped" : "db", "ok" : 1 }    

I like to use heredocs for things like this, in case you want more complex sequence of commands.

answered 5 years ago Gabriel Mancini #4

Other way:

echo "db.dropDatabase()" | mongo <database name>

answered 5 years ago Alex Baban #5

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 }

answered 5 years ago Anderson Lopes #6

Execute in a terminal:

mongo // To go to shell

show databases // To show all existing databases.

use <DATA_BASE> // To switch to the wanted database.

db.dropDatabase() // To remove the current database.

answered 5 years ago bhv #7

Here are some use full delete operations for mongodb using mongo shell

To delete particular document in collections: db.mycollection.remove( {name:"stack"} )

To delete all documents in collections: db.mycollection.remove()

To delete collection : db.mycollection.drop()

to delete database : first go to that database by use mydb command and then


directly from command prompt or blash : mongo mydb --eval "db.dropDatabase()

answered 4 years ago mikemaccana #8

You don't need heredocs or eval, mongo itself can act as an interpreter.

#!/usr/bin/env mongo
var db = new Mongo().getDB("someDatabase");

Make the file executable and run it.

answered 4 years ago user2719890 #9

Start MongoDB

Command for Database drop is :

1. first select the database which you want to delete

use < database name >

2. Then use this..


answered 4 years ago TechGuy #10

In you command prompt, First connect to mongodb using following command:

mongo -h [host-name]:[port:number] -d [dbname] -u [username] -p [password]

you will be accessing db with <dbname>.

Run the following command to drop the whole database:


answered 2 years ago sergiuz #11

Using Javascript, you can easily create a drop_bad.js script to drop your database:

create drop_bad.js:

use bad;

Than run 1 command in terminal to exectue the script using mongo shell:

mongo < drop_bad.js

answered 1 year ago Lakmal Vithanage #12

Eventhough there are several methods, The best way (most efficient and easiest) is using db.dropDatabase()

answered 1 year ago helcode #13

Open another terminal window and execute the following commands,

use mydb

Output of that operation shall look like the following

> show databases
local      0.78125GB
mydb       0.23012GB
test       0.23012GB
> use mydb
switched to db mydb
{ "dropped" : "mydb", "ok" : 1 }

Please note that mydb is still in use, hence inserting any input at that time will initialize the database again.

answered 7 months ago Anoop Sharma #14

use following command from mongo shell to drop db

use ; db.dropDatabase();

comments powered by Disqus