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.

share|improve this question

13 Answers 13

up vote 476 down vote accepted

In the mongo shell:

use [database];
db.dropDatabase();

Ruby code is pretty similar.

share|improve this answer
31  
Note: This will not delete the database, only all of its contents. – connorbode Jul 18 '15 at 18:13
13  
@connorbode Thanks for this. I've read it and immediately though: "B-But OP doesn't wants to remove the database!". Very misleading command !! – Henrique Miranda Nov 25 '15 at 13:56
    
Use with caution: if you're in a sharded environment using wiredTiger and you have no user database and you invoke dropDatabase, the database will be deleted and could re-appear as primary on a different shard when new records are added. – Jason R. Coombs Aug 4 '16 at 14:50
1  
This won't delete the user that is attached to related database. So you may want to delete it manually. db.dropAllUsers(); – Fırat KÜÇÜK Nov 11 '16 at 9:13
1  
Note that the database will not show after using the "use dbs" command. However, it is there. So, no worries. – wynshaft Apr 3 '17 at 17:45

Also, from the command line:

mongo DATABASE_NAME --eval "db.dropDatabase();"
share|improve this answer
1  
I don't believe this works in 2.4.6. My records still exist. – Brandon Clark Sep 1 '13 at 17:35

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

share|improve this answer
2  
As mentioned by @DanH you may find it more reliable to use remove in place of drop. The remove option appears to maintain constraints on fields the the collections that you are clearing. When we employed the drop method, the unique constraint on one of our fields was not honoured following the drop. – Scottymac Jan 16 '15 at 18:24
    
@Scottymac - better yet, add an else branch (to the if (c.indexOf("system.") == -1)) that does remove instead of drop. That way you're not left with empty collections if you're not using them anymore – Bogdan D Jan 21 '15 at 11:58
    
Better than db[c], use db.getCollection(c) which avoids errors when collection names are digits. – Jason R. Coombs Aug 1 '16 at 16:32
1  
According to the docs, since MongoDB 2.6, a dropDatabase command won't delete users, so the accepted answer is probably preferable. – Jason R. Coombs Aug 1 '16 at 16:37
    
If the collection name is numeric, then this should work instead: db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db.getCollection(c).drop(); }) – Constantin Galbenu Dec 8 '16 at 10:29

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()});"
share|improve this answer
    
Thank you for this suggestion, we were using db[collection_name].drop() and it was exhibiting the same issues you described with the db.dropDatabase() method. Switching the s/drop/remove/ worked brilliantly ! – Scottymac Jan 16 '15 at 16:35
4  
I found that remove() doesn't work well on MongoDB for Windows, and instead I needed to do remove({}) which works on both OSX and Windows. – DanH Jan 19 '15 at 3:23
    
Thanks for the tip, we are on a Linux platform, but this is worth looking into a bit further. – Scottymac Jan 19 '15 at 13:19
1  
I noticed an error for the delete - since db[collection_name].remove() doesn't have a query! So it actually needs to be: db[collection_name].remove({}) – JoelParke Oct 6 '16 at 22:47

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

Connect to you database, run the code.

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

share|improve this answer
    
This works quite well. Exactly what I needed. – David Betz Nov 22 '15 at 1:43

Use

[databaseName]
db.Drop+databaseName();

drop collection 

use databaseName 
db.collectionName.drop();
share|improve this answer

Hear 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()
share|improve this answer

if you want to delete only a database and its sub-collections use this :

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

if you want to delete all the databases in mongo then use this :

db.adminCommand("listDatabases").databases.forEach(function(d)
             {
              if(d.name!="admin" && d.name!="local" && d.name!="config")
                {
                 db.getSiblingDB(d.name).dropDatabase();
                }
             }
          );
share|improve this answer
    
Great answer... this is probably what the user was getting at – robert Oct 25 '17 at 22:45

Simplest way to delete a database say blog:

> use blog
switched to db blog
> db.dropDatabase();
{ "dropped" : "blog", "ok" : 1 }
share|improve this answer

For Meteor developers.

  1. Open a second terminal window while running your app in localhost:3000.

  2. In your project's folder run, meteor mongo.

    coolName = new Mongo.Collection('yourCollectionName');

  3. Then simply enter db.yourCollectionName.drop();

  4. You'll automatically see the changes in your local server.

For everybody else.

db.yourCollectionName.drop();

share|improve this answer

To delete all DBs use:

for i in $(mongo --quiet --host $HOSTNAME --eval "db.getMongo().getDBNames()" | tr "," " ");

do mongo $i --host $HOSTNAME --eval "db.dropDatabase()";

done 
share|improve this answer
use <dbname>
db.dropAllUsers()
db.dropAllRoles()
db.dropDatabase()

MongoDB db.dropDatabase() documentation explaining the modification introduced in 2.6:

Changed in version 2.6: This command does not delete the users associated with the current database.

share|improve this answer
  1. List out all available dbs show dbs
  2. Choose the necessary db use
  3. Drop the database db.dropDatabase() //Few additional commands
  4. List all collections available in a db show collections
  5. Remove a specification collection db.collection.drop()

Hope that helps

share|improve this answer

protected by user3100115 Oct 25 '16 at 5:29

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.