Last active
February 8, 2018 23:07
-
-
Save dmitry/cd348d79e742a50aa29a50f6ffd15837 to your computer and use it in GitHub Desktop.
Cleanup undeleted paperclip files from the filesystem
This file contains 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
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