Delete everything in a MongoDB database

Delete everything in a MongoDB database

In the mongo shell:

use [database];
db.dropDatabase();

And to remove the users:

db.dropAllUsers();

Also, from the command line:

mongo DATABASE_NAME --eval db.dropDatabase();

Delete everything in a MongoDB database

I had the same problem, when I needed to reset all the collections but didnt 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..

Leave a Reply

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