Best way to efficiently "reset" a mongodb replica set

Hi all,

I hope this is the correct section to ask my question: I’m developing a performance testing framework for MongoDB deployments, and I would like to efficiently reset a MongoDB replica set after every test run. My current solution is to simply terminate all the nodes, removes all the data and logs from the file system and redeploy the whole thing from scratch: this takes around 15 seconds, mostly due to the election process. Is it possible to simply instruct the deployment to “nuke” itself, without redeploying?

I found this code snippet on StackOveflow:

//drops user collections, clears system collections 
db.getCollectionNames().forEach( function(collection_name) {
    if (collection_name.indexOf("system.") == -1)

I was wondering if it is enough to guarantee a completely clean replica set

Thank you all

Hi @Zikker,

For a completely clean replica set (no oplog or local database history) with persistent storage, the approach you are taking is already the most efficient as long as re-deploying only refers to recreating the replica set config (i.e. you don’t need to reinstall the binaries).

An alternative which isn’t completely clean would be to call db.dropDatabase() on all non-system databases (excluding local, config, and admin). This would not clear system data like the replication oplog, sessions, and user/role configuration so doesn’t seem appropriate for performance testing.


1 Like

thank you, what about the code snippet that I have copy-pasted? does that clear the oplog, database history, indices, … ?

Hi @Zikker,

The code snippet you provided drops all collections in the current database. Dropping a collection includes the data and any associated indexes, but you cannot drop the collection or the local database while running in replica set mode. This leaves some significant history (eg past oplog entries) that could affect performance testing as compared to a clean install.

There is no supported method to “nuke” the data on a running replica set: shutting down all members, removing the dbpath contents, and then recreating the replica set is the most efficient approach.


fine, I’ll keep doing it the way I usually do then, thank you!

Have you looked at GitHub - rueckstiess/mtools: A collection of scripts to set up MongoDB test environments and parse and visualize MongoDB log files.?

I was introduced to this while taking M312: Diagnostics and Debugging from MongoDB University.

I think that


a script to quickly spin up local test environments, including replica sets and sharded systems (requires pymongo )

might be apropos.

Thanks, I’ll give it a look, although it will probably have the same issue as my custom scripts (Wait around 15 seconds for the election process to “settle in”, then start testing)

Totally true. But I just wanted to mentioned it because I feel it is a good tool that might help you in other circumstances.

Hi @Zikker,

mlaunch automates standing up a local test deployment (all members running on the same machine) so I expect that wouldn’t be suitable for performance testing.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.