Skip to content

Instantly share code, notes, and snippets.

@javascriptlove
Created May 22, 2018 13:07
Show Gist options
  • Save javascriptlove/64e9ad78eab4695fe19574610d15a515 to your computer and use it in GitHub Desktop.
Save javascriptlove/64e9ad78eab4695fe19574610d15a515 to your computer and use it in GitHub Desktop.
Remove orphan GridFS chunks from MongoDB
var i = 0;
var orphans = 0;
db.fs.chunks.find({}, { files_id: 1 }).forEach(function(chunk) {
i++;
if (i % 100 === 0) {
print(i);
}
if (!db.fs.files.findOne({ _id: chunk.files_id }, { _id: 1 })) {
print("orphan " + chunk._id);
db.fs.chunks.remove({ _id: chunk._id });
orphans++;
}
});
print("done, " + orphans + " orphans deleted");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment