Delete a bunch of mongo databases at once

I know this is knucklehead stuff, but I never trust myself to do it without 10 minutes of practice beforehand:

var dbs = db.getMongo().getDBNames()
var prefix_to_drop = 'testdb_';

for (var i in dbs) {
  db = db.getMongo().getDB(dbs[i]);
  if (db._name.lastIndexOf(prefix_to_drop,0) === 0) {
  print("Dropping " + db._name);
  db.dropDatabase();
  }
}

I use this when I’ve been breaking tests, and the database cleanup is getting skipped. And I end up with 150 test databases lying around.

Leave a Reply

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