Mongodb batch deletes gridfs file instances

  • 2020-05-14 05:16:56
  • OfStack

There are a lot of images on the platform that are no longer in use, orders of magnitude up to a million, about 100G of space, whether it's a waste or not?


db.fs.files.find({filename:/xxx/}).forEach(function(n) {db.fs.files.update({filename:u.filename},{$set:{filename:newname}},false,true)}} // The regular batch is changed to a fixed name for easy deletion.
mongofiles -port 12345 -d xxx delete newname //mongofies According to the filename Batch these files. db.repairDatabase() // Note in particular that the above deletions are not physical deletions, chunks The document is actually still there, show dbs or db.stats() found db the size No change. Execute db.repairDatabase() ; Note also that this action is a global write lock, which consumes memory and memory cpu , and the processing time is related to the number of file size, be sure to do in the off-peak time.

Almost 1 hour later over, comfortable, 100G space!!

Be aware that this command take a long to run if your is large In addition it requires a quantity of disk equal to the your database If you free space on the same volume, you can mount a volume and use that for the repair. In this case you must the command and use the, repairpath switch specify the the the temporary repair files
Warning: This command obtains a write lock and will operations until it has completed


Related articles: