How to remove existing content before entering a replicaset the first time?

mongodb worries about:

... has data already, cannot initiate set. All members except initiator must be empty.

2 Answers 2


db.dropDatabase() will drop the database, which will also drop all of the collections within a database.

If you need to see what databases you have, you can do show dbs.

Update: Here's a script to delete everything, make sure you really want to do this:

var dbs = db.getSisterDB('admin').adminCommand("listDatabases").databases
for(d in dbs) db.getSisterDB(dbs[d].name).dropDatabase();
  • 1
    WARNING: This will drop the admin and local db's also
    – Salakar
    Commented Apr 20, 2017 at 4:18

You can delete the contents of the mongodb data directories.

On Unix-like operating systems, using the default apths:

systemctl stop mongod   # or the equivalent if not using something with systemd
rm -r /var/lib/mongodb/*  # Double check that this is not a box with data you want to keep (i.e a secondary, not the initiator
systemctl start mongod  # or the relevant one for your init system

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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