Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup undeleted paperclip files from the filesystem
a = Dir[Rails.root.join('public/system/photos/original/*').to_s]; 1
not_exists = a.map {|v| v.match(/\/([^\/]+)\.[^\/]*\z/).to_a[1].to_i } - Photo.pluck(:id)
not_exists.count
files = Dir[Rails.root.join('public/system/photos/**/*').to_s]; 1
match = Regexp.new("\/(#{not_exists.join('|')})\\.[^\/]*\\z")
matched = files.select {|v| v =~ match }; 1
matched.map {|v| File.stat(v).size }.inject(:+)
# TO DELETE UNCOMMENT FileUtils.rm(matched)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.