Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
Mitch Dempsey Avatar asked Dec 28 '22 15:12

Mitch Dempsey


1 Answers

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.

like image 116
Roadmaster Avatar answered Jan 14 '23 21:01

Roadmaster