0

In my case, when users upload an image to S3 through Carrierwave (using fog) – their original image is processed and optimized copies are made and stored in S3 as well. However, when users delete these images – Carrierwave removes only the original, and the directory + optimized copies remain. I'd like to find a way to delete the original file, it's directory, and all files within.

I've found the following in Carrierwave's source, that I'm thinking might become helpful:

# File lib/carrierwave/storage/s3.rb, line 100
def delete
  AWS::S3::S3Object.delete @path, @uploader.s3_bucket
end

I'm wondering if anyone has accomplished something like this? I'd love to hear how you've done it... this could save quite a bit of disk in the long run.

cmw
  • 946
  • 2
  • 11
  • 26

1 Answers1

1

From Offical Documentation - Class: AWS::S3::ObjectCollection

there is an instance method delete for Object:

# delete 2 objects (by key) in a single request
bucket.objects.delete('abc', 'xyz')

You can delete objects also by passing their S3Object representation:

to_delete = []
to_delete << buckets.objects['foo']
to_delete << buckets.objects['bar']

bucket.objects.delete(to_delete)

Or to delete all objects from collection, there is an instance method delete_all:

(Array) delete_all

Deletes all objects represented by this collection.

Examples:

Delete all objects from a bucket

    bucket.objects.delete_all   
    Delete objects with a given prefix
    bucket.objects.with_prefix('2009/').delete_all