Skip to content

Instantly share code, notes, and snippets.

@dmitry
Last active February 8, 2018 23:07
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 dmitry/cd348d79e742a50aa29a50f6ffd15837 to your computer and use it in GitHub Desktop.
Save dmitry/cd348d79e742a50aa29a50f6ffd15837 to your computer and use it in GitHub Desktop.
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