-
-
Save coorasse/e355a9055474fe0b9aa509ccce12d4fc to your computer and use it in GitHub Desktop.
A Ruby on Rails script to delete files older than X days in a given directory.
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
# A Ruby On Rails script to delete files older than X days in a given directory. | |
# Quickly written by @coorasse | |
class FilesCleaner | |
def self.call(days = 1) | |
Dir.glob(Rails.root.join('path', 'to', 'folder', '*')).each do |filename| | |
File.delete(filename) if File.mtime(filename) < days.days.ago | |
end | |
end | |
end | |
# call with FilesCleaner.call |
Should add handling for subfolders in this folder otherwise, your script will fail.
Shouldn't it be File.mtime(filename) < X.days.ago
since you are comparing times? Less means older than.
If I want to delete files in rails public folder what should be the change in this
Dir.glob(Rails.root.join('path', 'to', 'folder', '*'))?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is pretty cool man