Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete everything in a MongoDB database

Tags:

mongodb

I'm doing development on MongoDB. For totally non-evil purposes, I sometimes want to blow away everything in a database—that is, to delete every single collection, and whatever else might be lying around, and start from scratch. Is there a single line of code that will let me do this? Bonus points for giving both a MongoDB console method and a MongoDB Ruby driver method.

like image 960
Trevor Burnham Avatar asked Jul 29 '10 19:07

Trevor Burnham


People also ask

How do I delete a whole collection in MongoDB?

To delete a MongoDB Collection, use db. collection. drop() command.

Does removing all collections in a database also remove the database MongoDB?

Yes, to all.

How do I delete multiple collections in MongoDB?

In MongoDB, you are allowed to delete the existing documents from the collection using db. collection. deleteMany() method. This method deletes multiple documents from the collection according to the filter.


9 Answers

In the mongo shell:

use [database]; db.dropDatabase(); 

And to remove the users:

db.dropAllUsers(); 
like image 187
Josh K Avatar answered Sep 27 '22 19:09

Josh K


Also, from the command line:

mongo DATABASE_NAME --eval "db.dropDatabase();"
like image 32
Rimian Avatar answered Sep 27 '22 17:09

Rimian


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

like image 32
Gering Avatar answered Sep 27 '22 17:09

Gering


I followed the db.dropDatabase() route for a long time, however if you're trying to use this for wiping the database in between test cases you may eventually find problems with index constraints not being honored after the database drop. As a result, you'll either need to mess about with ensureIndexes, or a simpler route would be avoiding the dropDatabase alltogether and just removing from each collection in a loop such as:

db.getCollectionNames().forEach(
  function(collection_name) {
    db[collection_name].remove()
  }
);

In my case I was running this from the command-line using:

mongo [database] --eval "db.getCollectionNames().forEach(function(n){db[n].remove()});"
like image 44
DanH Avatar answered Sep 27 '22 18:09

DanH


db.getCollectionNames().forEach(c=>db[c].drop())
like image 27
Selvakumar Ponnusamy Avatar answered Sep 27 '22 18:09

Selvakumar Ponnusamy


By compiling answers from @Robse and @DanH (kudos!), I've got the following solution which completely satisfies me:

db.getCollectionNames().forEach( function(collection_name) { 
  if (collection_name.indexOf("system.") == -1) 
       db[collection_name].drop();
  else  
       db[collection_name].remove({});
});

It cleans the database by dropping the user collections and emptying the system collections.

like image 34
Bogdan D Avatar answered Sep 27 '22 17:09

Bogdan D


Here are some useful 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 any particular collection : db.mycollection.drop()

to delete database : first go to that database by use mydb command and then

db.dropDatabase()
like image 33
bhv Avatar answered Sep 27 '22 19:09

bhv


in case you'd need to drop everything at once: (drop all databases at once)

mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'
like image 45
sjas Avatar answered Sep 27 '22 17:09

sjas


Use

[databaseName]
db.Drop+databaseName();

drop collection 

use databaseName 
db.collectionName.drop();
like image 30
Mohamed El Gamal Avatar answered Sep 27 '22 19:09

Mohamed El Gamal