Delete Folder on Amazon S3 using aws-sdk gem

I can delete individual files in the β€œFolder” on Amazon S3 using the following:

s3 = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_ACCESS_KEY']) folder_path = 'uploads/' +@image.s3 _filename s3.buckets[ENV['AWS_BUCKET']].objects.with_prefix(folder_path).delete_all 

but it leaves an empty folder. How can I just delete the entire folder (folder_path)?

+6
source share
3 answers

Everything on S3 is an object with which you can manipulate your β€œkey”. If you just take it, you can call delete on it:

 s3.buckets[ENV['AWS_BUCKET']].objects["name of the folder"].delete 
+6
source

This is an old question, but you can do it for aws-sdk 2.0>

 s3 = Aws::S3::Resource.new folder = 'path/to/the/folder' objects = s3.bucket(ENV['S3_BUCKET_NAME']).objects({prefix: folder}) objects.batch_delete! 

delete depreciated

Hope this helps!

+3
source

You must delete all files in the folder after deleting the folder.

+1
source

All Articles