Created
May 22, 2018 13:07
-
-
Save javascriptlove/64e9ad78eab4695fe19574610d15a515 to your computer and use it in GitHub Desktop.
Remove orphan GridFS chunks from MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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