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

Mongodb

Mongodb Problem Overview


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

Mongodb Solutions


Solution 1 - Mongodb

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

Solution 2 - Mongodb

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.

Solution 3 - Mongodb

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 }

Solution 4 - Mongodb

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.

Solution 5 - Mongodb

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()

Solution 6 - Mongodb

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.

Solution 7 - Mongodb

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()

Solution 8 - Mongodb

Other way:

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

Solution 9 - Mongodb

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.

Solution 10 - Mongodb

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.

Solution 11 - Mongodb

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
}

Solution 12 - Mongodb

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

Solution 13 - Mongodb

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()

Solution 14 - Mongodb

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

Solution 15 - Mongodb

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()"

Solution 16 - Mongodb

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();'

Solution 17 - Mongodb

LogIn into your mongoDB command line: And type the below commands. use "YOUR_DATABASE_NAME"; db.dropDatabase();

Solution 18 - Mongodb

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")

Solution 19 - Mongodb

In order to be really sure that you drop the correct database use

mongo <connection properties> --eval "db.getSiblingDB('dbname').dropDatabase()" 

See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 to understand the concerns.

Solution 20 - Mongodb

use following command from mongo shell to drop db

use <database name>; 
db.dropDatabase();

Solution 21 - Mongodb

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();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncoffee-grinderView Question on Stackoverflow
Solution 1 - MongodbTim GautierView Answer on Stackoverflow
Solution 2 - MongodbmnemosynView Answer on Stackoverflow
Solution 3 - MongodbAlex BabanView Answer on Stackoverflow
Solution 4 - MongodbmikemaccanaView Answer on Stackoverflow
Solution 5 - Mongodbuser2719890View Answer on Stackoverflow
Solution 6 - MongodbDavidView Answer on Stackoverflow
Solution 7 - MongodbbhvView Answer on Stackoverflow
Solution 8 - MongodbGabriel ManciniView Answer on Stackoverflow
Solution 9 - MongodbAnderson LopesView Answer on Stackoverflow
Solution 10 - MongodbhelcodeView Answer on Stackoverflow
Solution 11 - MongodbShreyaView Answer on Stackoverflow
Solution 12 - MongodbsergiuzView Answer on Stackoverflow
Solution 13 - MongodbTechGuyView Answer on Stackoverflow
Solution 14 - MongodbLakmal VithanageView Answer on Stackoverflow
Solution 15 - MongodbjasenmichaelView Answer on Stackoverflow
Solution 16 - MongodbBuzz MoschettiView Answer on Stackoverflow
Solution 17 - MongodbShrinivas KalangutkarView Answer on Stackoverflow
Solution 18 - MongodbJoe DrumgooleView Answer on Stackoverflow
Solution 19 - MongodbWernfried DomscheitView Answer on Stackoverflow
Solution 20 - MongodbAnoop SharmaView Answer on Stackoverflow
Solution 21 - Mongodbnixxo_raaView Answer on Stackoverflow