Paperclip - delete file from Amazon S3?

I need to be able to delete files from S3 that are stored by users, such as profile photos. Just the call @user.logo.destroydoesn’t seem to do the trick - I get [paperclip] Saving attachments.in the logs and the file stays right there in the S3 bucket.

How to delete the file itself?

+5
source share
2 answers

These are paper clip methods that you can use to remove attachments:

module Paperclip
  class Attachment
    # Clears out the attachment. Has the same effect as previously assigning  
    # nil to the attachment. Does NOT save. If you wish to clear AND save,
    # use #destroy.
    def clear
      queue_existing_for_delete
      @errors            = {}
    end

    # Destroys the attachment. Has the same effect as previously assigning
    # nil to the attachment *and saving*. This is permanent. If you wish to
    # wipe out the existing attachment but not save, use #clear.
    def destroy
      clear
      save
    end

So you see that destruction only removes the attachment if an error does not occur. I tried this with my own installation against S3, so I know it breaks the job.

, - , ? I.e validates_attachment_presence - ?

, @user.logo.destroy, @user.errors, , .

+2

, destroy clear ( , has_attached_file, ):

Rails Paperclip ?

+1

All Articles