Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Last active October 30, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/471b59c61c42c136935fc7878e3a1938 to your computer and use it in GitHub Desktop.
Save AvnerCohen/471b59c61c42c136935fc7878e3a1938 to your computer and use it in GitHub Desktop.
Mongo Query to Delete Celery succesfull entries on a large collection, slowly but surely.
var COUNTER = 900;
function deleteSome(count) {
var itemsToDel = db.tasks.find({status: "SUCCESS"}, {_id: 1}).sort({created_at: 1}).limit(count).toArray();
var IDs = itemsToDel.map(function(item){ return item["_id"]})
if (IDs.length > 0 ) {
db.tasks.remove({"_id": {"$in": IDs } });
sleep(5000);
} else {
COUNTER++;
print("delete skipped - " + COUNTER);
}
}
//LOG BEFORE
var beforeCount = db.tasks.count();
print(beforeCount)
//USAGE
for (; COUNTER > 0; COUNTER--) { deleteSome(2000)}
//COUNTER
var afterCount = db.tasks.count();
print("Reduced" + (beforeCount - afterCount) + ", And still to go - " + afterCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment