Delete everything in a MongoDB database

Also, from the command line:

mongo DATABASE_NAME --eval "db.dropDatabase();"

I had the same problem, when I needed to reset all the collections but didn't want to loose any database users. Use the following line of code, if you would like to save the user configuration for the database:

use <whichever database>
db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop(); })

This code will go through all collection names from one database and drop those which do not start with "system.".


In the mongo shell:

use [database];
db.dropDatabase();

And to remove the users:

db.dropAllUsers();

Tags:

Mongodb