MongoDB drop every database

Mongodb

Mongodb Problem Overview


I would like to know if there're a command to drop every databases from my MongoDB?

I know if I want to drop only one datatable, I just need to type the name of the database like the code below but I dont want to have to specify it.

mongo DB_NAME --eval 'db.dropDatabase();'

Mongodb Solutions


Solution 1 - Mongodb

you can create a javascript loop that do the job and then execute it in the mongoconsole.

var dbs = db.getMongo().getDBNames()
for(var i in dbs){
    db = db.getMongo().getDB( dbs[i] );
    print( "dropping db " + db.getName() );
    db.dropDatabase();
}

save it to dropall.js and then execute:

mongo dropall.js

Solution 2 - Mongodb

Try this command:

mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'

Solution 3 - Mongodb

You can also do this with a simple mongo command:

db.adminCommand("listDatabases").databases.forEach( function (d) {
    if (d.name != "local" && d.name != "admin"  && d.name != "apiomat"  && d.name != "config")
        db.getSiblingDB(d.name).dropDatabase();
 })

Solution 4 - Mongodb

db
  .getMongo()
  .getDBNames()
  .filter(n => !['admin','local','config'].includes(n))
  .forEach(dname =>
    db
      .getMongo()
      .getDB(dname)
      .dropDatabase()
  )
;
db.getMongo().getDBNames().filter(n => !['admin','local','config'].includes(n)).forEach(dname => db.getMongo().getDB(dname).dropDatabase());

This drops all DBs but preserves MongoDB's internal collections.

Solution 5 - Mongodb

Save this to drop_all_dbs.js:

var databases = db.getMongo().getDBNames()
for(var i in databases){
    db = db.getMongo().getDB( databases[i] );
    if(db.getName() == "admin" || db.getName() == "local"){
        print("skipping db " + db.getName())
        continue
    }
    print( "dropping db " + db.getName() );
    db.dropDatabase();
}

Now you can execute:

mongo drop_all_dbs.js

and all databases (except for admin and local) will be dropped.

This answer is a copy of ALoR's one, just fix drop of system dbs

Solution 6 - Mongodb

Adding to @ALoR's answer, for convenience you can put the following in ~/.mongorc.js

function dropDatabases(){
    var mongo = db.getMongo();

    var dbNames = mongo.getDBNames();
    for (var i = 0; i < dbNames.length; i++) {
        var db = mongo.getDB( dbNames[i] );

        print( "Dropping database " + db.getName() + "..." );
        db.dropDatabase();
    }
}

Then at the mongo shell you can simply do

dropDatabases()

From the docs:

> Mongo will read the .mongorc.js file from the home directory of the user invoking mongo. In the file, users can define variables, customize the mongo shell prompt, or update information that they would like updated every time they launch a shell.

Solution 7 - Mongodb

You can do it easy through c# official driver:

var _mongoServer = MongoServer.Create("mongodb://localhost:27020");

var names = _mongoServer.GetDatabaseNames();
foreach (var name in names)
{
   _mongoServer.DropDatabase(name);
}

Solution 8 - Mongodb

This is what worked best for me.

Save this to a file called drop_most_databases.js:

const databasesToSkip = ['admin', 'local', 'config'];
db.getMongo()
  .getDBNames()
  .forEach((dbname) => {
    if (databasesToSkip.includes(dbname)) {
      print(`Skipping ${dbname}`);
    } else {
      print(`Dropping ${dbname}...`);
      db.getMongo().getDB(dbname).dropDatabase();
    }
  });
print(`Now, the only databases that remain are:`);
db.getMongo()
  .getDBNames()
  .forEach((dbname) => {
    print(dbname);
  });

From Terminal, run mongo drop_most_databases.js.

Solution 9 - Mongodb

Here is a oneliner that allows you to filter for specific names:

for (const dbName of db.getMongo().getDBNames().filter(dbName => dbName.startsWith('test-'))) {console.log(dbName); const dbToDelete = db.getMongo().getDB( dbName ); dbToDelete.dropDatabase()}

Solution 10 - Mongodb

It is as easy as

mongo --eval 'db.dropDatabase()'

Or, you can start a mongo session on your terminal and write

db.dropDatabase()

Which is exactly the same.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - MongodbALoRView Answer on Stackoverflow
Solution 2 - MongodbkevView Answer on Stackoverflow
Solution 3 - MongodbAndreasView Answer on Stackoverflow
Solution 4 - Mongodbvu leView Answer on Stackoverflow
Solution 5 - MongodbGasView Answer on Stackoverflow
Solution 6 - MongodbbtiernayView Answer on Stackoverflow
Solution 7 - MongodbAndrew OrsichView Answer on Stackoverflow
Solution 8 - MongodbRyanView Answer on Stackoverflow
Solution 9 - MongodbphilkunzView Answer on Stackoverflow
Solution 10 - MongodbPedro José Piquero PlazaView Answer on Stackoverflow