Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files from folder in S3 bucket

I have an AWS S3 bucket test-bucket with a data folder. The data folder will have multiple files.

I am able to delete the files in the S3 bucket. But what I want is to delete the files in the data folder without deleting the folder.

I tried the following:

aws s3 rm  s3://test-bucket/data/*

Also checked using --recursive option, but that does not work.

Is there a way I can delete the files in the folder using AWS CLI?

like image 209
cooldev Avatar asked Sep 15 '17 19:09

cooldev


People also ask

How do I delete files from Amazon S3 bucket?

If you no longer need to store the file you've uploaded to your Amazon S3 bucket, you can delete it. Within your S3 bucket, select the file that you want to delete, choose Actions, and then choose Delete. In the confirmation message, choose OK.

How do I delete a folder from my AWS S3?

In 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.

What is the best way to delete multiple objects from S3?

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.


2 Answers

Following aws cli command worked:

aws s3 rm s3://test-bucket --recursive --exclude="*" --include="data/*.*"
like image 183
cooldev Avatar answered Oct 11 '22 08:10

cooldev


you can do it using aws cli : https://aws.amazon.com/cli/ and some unix command.

this aws cli commands should work:

aws s3 rm s3://<your_bucket_name> --exclude "*" --include "<your_regex>"

if you want to include sub-folders you should add the flag --recursive

or with unix commands:

aws s3 ls s3://<your_bucket_name>/ | awk '{print $4}' | xargs -I%  <your_os_shell>   -c 'aws s3 rm s3:// <your_bucket_name>/% $1'

explanation: list all files on the bucket --pipe--> get the 4th parameter(its the file name) --pipe--> run delete script with aws cli

like image 25
ggcarmi Avatar answered Oct 11 '22 07:10

ggcarmi