Skip to content

How to cleanup unused uploads

assembler edited this page Feb 2, 2013 · 5 revisions

As of version 1.2.2 attachinary is automatically tagging uploads with "attachinary_tmp" until they are saved in database (at which point the tag is removed). If the user abandons the page in the meantime, uploads remain "floating" on your cloudinary account.

You can cleanup these unused images with following sample code:

resources = Cloudinary::Api.resources_by_tag(Attachinary::TMPTAG)["resources"]

ids = resources.select { |r| r['created_at'].to_time < 2.hours.ago }
               .map    { |r| r['public_id'] }

Cloudinary::Api.delete_resources(ids, resource_type: 'auto') unless ids.empty?

It takes all resources which are tagged with attachinary_tmp and filters only those that are created more then 2 hours ago, and then deletes them.

You can create scheduled rake task, or use background worker, its up to you.

Clone this wiki locally