Skip to content

Drop a MongoDB database from the command line

You can drop a database in MongoDB from the command line using db.dropDatabase() command or a one line shell script. Let’s see how to do this.

db.dropDatabase() command removes the current database, deleting the associated data files. This by default delete default ‘test’ database. If you have selected any database using use <db> command, then db.dropDatabase() command will drop that particular database.

You can do it in two ways.

Mongo console

> show dbs
admin      0.000GB
config     0.000GB
local      0.000GB
chat-bot  0.019GB
orders   0.000GB
> use orders
> db.dropDatabase()
{ "ok" : 1 }
> show dbs
admin      0.000GB
config     0.000GB
local      0.000GB
chat-bot  0.019GB

Command line as one line script

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

See also  MongoError: the update operation document must contain atomic operators - How to fix?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.