Skip to content

How to: Cleanup original file that wasn't uploaded like as Tempfile

kavu edited this page Nov 24, 2011 · 2 revisions

In case, when your file was uploaded for example through nginx upload module, it won't be removed like it would be as Tempfile(Rails, Rack uploaders create this instance for uploaded files). For this situation you could add a callback to the your uploader class:

after :cache, :unlink_original

def unlink_original(file)
  return unless delete_original_file
  file.delete if version_name.blank?
end

config/initializers/carrierwave.rb:

class CarrierWave::Uploader::Base
  add_config :delete_original_file
end

CarrierWave.configure do |config|
  config.delete_original_file = true
end

For tests you could set deletion to false:

ImageUpload.delete_original_file = false
Clone this wiki locally