Skip to content

Instantly share code, notes, and snippets.

@halloffame
Created February 6, 2015 16:55
Show Gist options
  • Save halloffame/cda92400704a4196de9b to your computer and use it in GitHub Desktop.
Save halloffame/cda92400704a4196de9b to your computer and use it in GitHub Desktop.
Ruby script to clean up all the log and temp files in your rails projects.
# Running this file will clean up all of the temp files in your rails projects
# Instructions:
# 1. Save the file wherever you want
# 2. Replace the RAILS_PATH with the path to your rails projects
# 3. Run `ruby this_file_name.rb`
#
RAILS_PATH = '/replace/with/your/path/to/rails/projects'
require 'rake'
# Truncates all *.log files in log/ to zero bytes
# Pulled from `rake log:clear`
def clear_log_files
FileList["log/*.log"].each do |file|
f = File.open(file, "w")
f.close
end
end
# Clear cache and socket files from tmp/
# Pulled from `rake tmp:clear`
def clear_cache
FileUtils.rm_rf(Dir['tmp/cache/[^.]*']) # Clears all files and directories in tmp/cache
FileUtils.rm(Dir['tmp/sockets/[^.]*']) # Clears all files in tmp/sockets
end
Dir.new(RAILS_PATH).each do |dir|
next if dir.match(/^\./) # Skip any file that starts with a .
full_path = "#{RAILS_PATH}/#{dir}"
puts "#{dir}"
Dir.chdir(full_path){
# puts %x[rake tmp:clear log:clear]
clear_log_files
clear_cache
}
end
puts "All done. Calculating new file sizes..."
Dir.chdir(RAILS_PATH){
puts %x[du -sh *]
}
@amnesia7
Copy link

amnesia7 commented Dec 17, 2017

Hi,

This script falls over for the file config.ru that I have top-level.
I'm not sure if it would also fall over for the file rubocop.yml that is also in the main project folder.
Is there an issue with the regex to match files that start with .?

Also, is require 'rake' still needed since the rake calls seem to be commented out.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment