What's the easiest way to do this from my bash prompt?
-
5see this question: http://stackoverflow.com/questions/3366397/delete-everything-in-a-mongodb-database. It's really easy to do it from the mongo shell. – Jesse Pollak Jan 13 '12 at 21:16
22 Answers
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
Edit:
In Mongo 6.0 mongo
was removed and replaced with mongosh
which has to be installed separately. More info here: https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh
The equivalent in mongosh
would be:
mongosh --eval "use <dbname>" --eval "db.dropDatabase()"

- 129
- 1
- 8

- 29,150
- 5
- 46
- 53
-
This was very helpful in my case. Somehow I had a database named "mean-dev" from an older installation, and entering "mean-dev.dropDatabase()" in the mongo shell resulted in "ReferenceError: mean is not defined". But using the solution in this answer did the trick. +1 – K. P. MacGregor Aug 26 '14 at 01:15
-
22If you want to get a human readable result, do it this way: ```mongo
--eval "printjson(db.dropDatabase())"``` – Xentatt Sep 07 '14 at 11:23 -
1K. P. MacGregor. you should have been: "use mean-dev" + "db.dropDatabase()" – ozma Feb 23 '16 at 15:14
-
1Thanks. This is how you can customise host and port: `mongo
--host – Clemens Sep 17 '19 at 10:20--port --eval 'db.dropDatabase()'` -
1Now, when "mongo" was removed, you can do it like this: `mongosh
--eval "printjson(db.dropDatabase())"`. – Eugene Feb 15 '23 at 03:36
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.

- 143,271
- 52
- 317
- 404

- 45,391
- 6
- 76
- 82
-
3I think he means directly from the prompt doesn't he? In that case you either have to generate a .js file with those commands and invoke it from the prompt using "mongo dropdb.js" or something or do as you say and remove the files manually. – Remon van Vliet Jan 13 '12 at 21:18
-
2but be aware that you cannot use `use` command in .js file, you have to connect to concrete DB (specify dbname for mongo command) – Betlista Nov 29 '12 at 08:34
-
49Pro Tip: after dropping a database, you might want to exit the mongo shell and delete your `~/.dbshell` file to clear your command history. (There may be a better way of doing that - I'm not sure.) I do this because, unlike with SQL, the mongo command to drop a database _does not actually reference the name of the database to drop_ - it just drops the database to which the client is currently connected. Clearing your command history will prevent your from accidentally replaying the `dropDatabase` command and unintentionally dropping a second database. – Chris Allen Lane Aug 08 '13 at 14:11
-
3That's a smart idea, @chrisallenlane. The mongodb shell is quite a dangerous tool at times... :) – mnemosyn Aug 08 '13 at 20:09
-
1**Warning:** If you drop a database and create a new database with the same name, you must either restart all mongos instances, or use the flushRouterConfig command on all mongos instances before reading or writing to that database. This action ensures that the mongos instances refresh their metadata cache, including the location of the primary shard for the new database. Otherwise, the mongos may miss data on reads and may write data to a wrong shard. – Lekr0 Mar 29 '19 at 12:34
-
@Lekr0 In MongoDB version 4.4 and newer the `flushRouterConfig` is not required anymore, see [Improved Routing Table Updates](https://docs.mongodb.com/manual/release-notes/4.4/#improved-routing-table-updates) – Wernfried Domscheit Feb 17 '22 at 14:36
-
Please, anybody, don't be stupid as me and stop all the programs running in the background that can be possibly using Mongo because it can (obviously) create the database you just removed and make it look like the dropDatabase() command doesn't work at all. – Banik Jul 05 '22 at 12:40
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 }

- 11,312
- 4
- 30
- 44
You don't need heredocs or eval, mongo
itself can act as an interpreter.
#!/usr/bin/env mongo
var db = new Mongo().getDB("someDatabase");
db.dropDatabase();
Make the file executable and run it.

- 110,530
- 99
- 389
- 494
-
3Note that the script name must end with `.js`, otherwise it will be interpreted as the db address. – Vegard Jan 02 '17 at 14:52
-
2@Vegard That's not correct. You're running `mongo filename` which is unnecessary - the file already has an interpreter in the top line. Just make the file executable and run it `./filename` – mikemaccana Jan 04 '17 at 10:39
-
@mikemaccana I know that, but doing `./filename` will actually run `/usr/bin/env mongo filename`, right? And so if `filename` doesn't end in `.py`, mongo will not recognise the argument as a script to run. – Vegard Jan 04 '17 at 11:00
-
"Doing ./filename will actually run /usr/bin/env mongo filename, right?" No. Check the output of `ps`. – mikemaccana Jan 04 '17 at 11:21
-
@mikemaccana The output of `ps` only shows you what's running at the time you invoke `ps`, not the chain of `exec` calls that lead to that point nor the work the kernel has done to load and execute the file. If you want to know what's really going on, you should use write a wrapper around `/usr/bin/env`, set that as the executable in the shebang line, and then kick the whole thing off with `strace`. – kbolino Mar 10 '17 at 23:02
-
@Vegard that is correct (except that it needs to be `.js` and not `.py`) and it will fail if your filename doesn't end in `.js` – kbolino Mar 10 '17 at 23:03
-
@kbolino ack re strace or systemtap being better than a ps point-in-time, but Vegard's statement that this will attempt to run `/usr/bin/env mongo filename` and therefore the file must end in `.py` is provably incorrect. – mikemaccana Mar 13 '17 at 11:26
-
@mikemaccana only because it should be `.js` -- the kernel will execute the equivalent of `/usr/bin/env mongo filename` which will eventually lead to a call to `mongo filename` for some `mongo` on the path, which means the filename will be treated as the database name, not the script to execute, unless it ends in `.js`, per the docs (and the docs are accurate on this point, at least for 3.x) – kbolino Mar 15 '17 at 02:57
-
@mikemaccana Regardless of what actually gets executed, the need for the file name to end in `.js` is testable (again, at least on 3.x). Create a script called `foo`, copy and paste the exact content of your answer, `chmod +x foo`, then run `./foo`. It will fail and the failure message will include `connecting to: mongodb://./foo`. Change the file name to `foo.js` and it works. – kbolino Mar 15 '17 at 03:03
-
@kbolino The interpreter is handled by Unix/Linux and not Mongo, and the answer has been tested. Unless Mongo itself is now a python interpreter rather than a JS one in Mongo 3, the result shouldn't change. – mikemaccana Mar 16 '17 at 10:16
-
@mikemaccana It has nothing to do with Python (that was just a typo many comments ago). The filename has to end in `.js` or Mongo won't interpret it *at all* because it treats the argument as the name of the database to use, not the script to execute. – kbolino Mar 17 '17 at 22:35
-
@kbolino Repeating: interpreters are handled by Unix/Linux and not Mongo. Also repeating: try it. – mikemaccana Mar 15 '18 at 14:00
-
@mikemaccana A year ago, I had tried it with 3.0.5. I just tried it again with 3.4.6. Nothing has changed in a year. The file name must end with `.js`. I have posted the exact steps to demonstrate the problem. I respectfully request that *you* try what I've done and see for yourself. – kbolino Mar 20 '18 at 20:42
-
for absolute clarity, here is a sequence of shell commands that will demonstrate the problem: https://pastebin.com/Tpt474te – kbolino Mar 20 '18 at 20:52
-
[mongo/shell/shell_options.cpp:400](https://github.com/mongodb/mongo/blob/73a74e4ba33af61b2f102ddf11e674ee30dc2768/src/mongo/shell/shell_options.cpp#L400) shows the argument parsing logic and why `.js` is required; but, it also shows that another way to solve this problem is to make the shebang line `#!/usr/bin/env mongo --nodb` -- then you can use whatever file extension you like (or none at all) – kbolino Apr 30 '18 at 22:08
Start MongoDB
Command for Database drop is :
1. first select the database which you want to delete
use < database name >
2. Then use this..
db.dropDatabase()

- 1,511
- 1
- 20
- 31
You could also use a "heredoc":
mongo localhost/db <<EOF
db.dropDatabase()
EOF
Results in output like:
mongo localhost/db <<EOF
db.dropDatabase()
EOF
MongoDB shell version: 2.2.2
connecting to: localhost/db
{ "dropped" : "db", "ok" : 1 }
bye
I like to use heredocs for things like this, in case you want more complex sequence of commands.

- 639
- 6
- 5
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
db.dropDatabase()
directly from command prompt or blash : mongo mydb --eval "db.dropDatabase()

- 5,188
- 3
- 35
- 44
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.

- 10,520
- 15
- 66
- 111

- 639
- 7
- 10
-
1Same answer as [this one](http://stackoverflow.com/questions/8857276/how-do-i-drop-a-mongodb-database-from-the-command-line/18997305#18997305), which was given a month earlier. – Dan Dascalescu Sep 09 '16 at 18:50
Open another terminal window and execute the following commands,
mongodb
use mydb
db.dropDatabase()
Output of that operation shall look like the following
MAC:FOLDER USER$ mongodb
> show databases
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
> use mydb
switched to db mydb
>db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
>
Please note that mydb
is still in use, hence inserting any input at that time will initialize the database again.

- 1,859
- 1
- 13
- 32
Using Javascript, you can easily create a drop_bad.js script to drop your database:
create drop_bad.js:
use bad;
db.dropDatabase();
Than run 1 command in terminal to exectue the script using mongo shell:
mongo < drop_bad.js

- 5,353
- 1
- 35
- 51
Open a terminal and type:
mongo
The below command should show the listed databases:
show dbs
/* the <dbname> is the database you'd like to drop */
use <dbname>
/* the below command will delete the database */
db.dropDatabase()
The below should be the output in the terminal:
{
"dropped": "<dbname>",
"ok": 1
}
one liner remote remove all collections from mongo database
note must use --host, (-h is help for mongo command), and -d is not an option, select the db and command after password.
mongo --host <mongo_host>:<mongo_port> -u <db_user> -p <db_pass> <your_db> --eval "db.dropDatabase()"

- 13,962
- 6
- 55
- 80

- 388
- 5
- 17
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:
db.dropDatabase()
-
-h is help, -d is not an option more like ` mongo --host [host-name]:[port:number] -u [username] -p [password] [dbname] --eval "db.dropDatabase()" ` – jasenmichael Jul 14 '19 at 07:28
Eventhough there are several methods, The best way (most efficient and easiest) is using db.dropDatabase()

- 2,767
- 7
- 42
- 58
Surprised that we haven't seen this variation come up. This minimizes extra args on the command line and explicitly shows the DB being switched to FOO
and then dropped:
$ mongo --host "mongodb://machine:port" --eval 'db.getSiblingDB("FOO").dropDatabase();'

- 7,057
- 3
- 23
- 33
LogIn into your mongoDB command line: And type the below commands. use "YOUR_DATABASE_NAME"; db.dropDatabase();

- 983
- 1
- 9
- 16
In order to be really sure that you drop the correct database use
mongo <connection properties> --eval "db.getSiblingDB('dbname').dropDatabase()"
See Authentication failure while trying to save to mongodb to understand the concerns.

- 54,457
- 9
- 76
- 110
use following command from mongo shell to drop db
use <database name>;
db.dropDatabase();

- 9,564
- 146
- 81
- 122

- 381
- 3
- 3
-
how does this command specify which database to drop? With a database called "auth-users" I tried `> use auth-users ; db.dropDatabase()` and `> use auth-users; db.dropDatabase()` and both returned `[thread1] Error: [auth-users ; db.dropD atabase()] is not a valid database name :` – MmmHmm Apr 08 '18 at 05:03
db will show the current Database name type: db.dropDatabase();
1- select the database to drop by using 'use' keyword.
2- then type db.dropDatabase();

- 391
- 8
- 21
Drop a MongoDB database using python:
import argparse
import pymongo
if __name__ == "__main__":
"""
Drop a Database.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--host", default='mongodb://localhost:27017',
help="mongodb URI [default: %(default)s]")
parser.add_argument("--database", default=None,
help="database name: %(default)s]")
args = parser.parse_args()
client = pymongo.MongoClient(host=args.host)
if args.database in client.list_database_names():
client.drop_database(args.database)
print(f"Dropped: '{args.database}'")
else:
print(f"Database '{args.database}' does not exist")

- 1,268
- 9
- 9
You can first switch to your database which you want to delete. Then you can delete the same by using the command dropDatabase().
Code :
>use dbName
>db.dropdataBase()
The result will be :
{ "dropped" : "dbName", "ok" : 1 }
If you want to delete a specific collection in a database, then switch to the database and enter the following command.
Code:
>use dbName
db.collection.drop()
The result will be :
true
If you want a better understanding of MongoDB shell commands it's better to follow the documentation always.
Link to the documentation : https://www.mongodb.com/docs/manual/reference/method/#std-label-js-administrative-methods

- 89
- 4