Skip to content

How to: Delete cache garbage directories

clyfe edited this page May 31, 2011 · 8 revisions

The tmp cache directory can fill up with empty dirs if it's not cleaned up. See issue #338.

To fix this add some callbacks in your uploader like in the following example

class FileUploader < CarrierWave::Uploader::Base
  storage :file
  
  before :store, :remember_cache_id
  after :store, :delete_tmp_dir

  # store! nil's the cache_id after it finishes so we need to remember it for deletition
  def remember_cache_id(new_file)
    @cache_id_was = cache_id
  end
  
  def delete_tmp_dir(new_file)
    # make sure we don't delete other things accidentally by checking the name pattern
    if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
      FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
    end
  end
end
Clone this wiki locally