Skip to content

Instantly share code, notes, and snippets.

@cvan
Last active October 4, 2023 19:16
Show Gist options
  • Save cvan/9743b3fc5adfe07cf1d684d640575d11 to your computer and use it in GitHub Desktop.
Save cvan/9743b3fc5adfe07cf1d684d640575d11 to your computer and use it in GitHub Desktop.
mongodb delete all documents in every collection
mongosh 'mongodb+srv://YOUR_MONGODB_ATLAS_URL.mongodb.net/' --apiVersion 1 --username YOUR_ADMIN_USERNAME
> Enter password:
(enter password)
use my-database-name
db.getCollectionNames().forEach((collection_name) => {
  if (collection_name.indexOf("system.") > -1) {
    return;
  }
  print("\n----------------------------------------");
  print(["COLLECTION:", collection_name].join('\n'));
  print(
    ["    Removing ", db[collection_name].count({}), " documents …"].join("")
  );
  db[collection_name].remove({});
  print(["    Count: ", db[collection_name].count({}), " documents"].join(""));
  print("----------------------------------------\n");
});
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment