Carrierwave - destroy object only after mounted files have been deleted from storage

1.6k views Asked by At

Carrierwave deletes files after the destruction of the object has been completed:

after_commit :remove_avatar! :on => :destroy

https://github.com/carrierwaveuploader/carrierwave

I have a worker that deletes the files. If one of the workers timeout when deleting a file from S3 I lose track of the files on S3 and my bucket becomes a mess (since my object that had the mounted file is gone from my DB).

How should I handle this? Directly call remove_avatar! before my object.destroy and then skip_callback? Is it safe?

1

There are 1 answers

1
Ivan Danci On
class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader

  before_destroy :clean_s3

private
  def clean_s3
    avatar.remove!
    avatar.thumb.remove! # if you have thumb version or any other version
  rescue Excon::Errors::Error => error
    puts "Something gone wrong"
    false
  end
end