Delete files from folder in S3 bucket

Viewed 13542

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?

3 Answers

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

aws s3 rm s3://bucket/folder1/folder2/ --recursive --dryrun

From what I see happening when I try it, adding the slash at the end means delete below folder2, not including folder2.

Related