Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete "Folder" on Amazon S3 with aws-sdk gem

I'm able to delete individual files within a "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/'[email protected]_filename
s3.buckets[ENV['AWS_BUCKET']].objects.with_prefix(folder_path).delete_all

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

like image 837
scientiffic Avatar asked Jan 30 '14 20:01

scientiffic


People also ask

How do I delete a folder from my AWS S3?

To delete folders from an S3 bucketIn the Buckets list, choose the name of the bucket that you want to delete folders from. In the Objects list, select the check box next to the folders and objects that you want to delete. Choose Delete.

How do I delete items from my AWS S3?

To delete the object, select the object, and choose delete and confirm your choice by typing delete in the text field. On, Amazon S3 will permanently delete the object version. Select the object version that you want to delete, and choose delete and confirm your choice by typing permanently delete in the text field.

Does S3 lifecycle delete folders?

The subfolders DO get deleted when the retention policy set to: "This rule applies to all objects in the bucket".

How do I delete an S3 bucket containing many objects?

Navigate to the Amazon S3 bucket or folder that contains the objects that you want to delete. Select the check box to the left of the names of the objects that you want to delete. Choose Actions and choose Delete from the list of options that appears. Alternatively, choose Delete from the options in the upper right.


1 Answers

This is an old question, but you can do this 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 was depreciated

Hope this helps!

like image 153
Jeremie Avatar answered Sep 25 '22 10:09

Jeremie