Mitch Dempsey
Mitch Dempsey

Reputation: 39869

How to get paperclip to delete files

I have a model that is using Paperclip to manage the file.

After I delete the model, I obviously would like the file to be deleted as well, but I cannot seem to find out how to get the file deleted using Paperclip.

I have tried self.sourcefile = nil if !sourcefile.dirty? in the before_destroy def, but that had no effect.

(I want to be able to have it delete the file locally when I test, and then on S3 when I use that - So i need a pure paperclip solution)

Any ideas?

Upvotes: 4

Views: 6979

Answers (2)

Roadmaster
Roadmaster

Reputation: 5357

Paperclip is pretty noisy when it fails to delete a file.

Without any special configuration, when you delete an object, the associated files, if any, are deleted too, and this is noted in the log (at least in development mode):

[paperclip] Deleting attachments.
[paperclip] deleting /clipper/public/system/avatars/3/original/whatever.JPG

If Paperclip is unable to delete the file, it will simply throw an exception.

All this was tested with the latest Paperclip (cloned from github on May 25th). If you have an older version you might want to update it, then run some tests while watching the logs.

Upvotes: 10

theIV
theIV

Reputation: 25774

I may be wrong but I believe it should do it automatically since has_attached_file adds a before_destroy callback. Source.

Edit: If it's not working, maybe you could try calling destroy_attached_files yourself, which would seem weird to me. Source.

Upvotes: 0

Related Questions