Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3: How to delete all contents of a directory in a bucket but not the directory itself?

I have an AWS S3 bucket entitled static.mysite.com

This bucket contains a directory called html

I want to use the AWS Command Line Interface to remove all contents of the html directory, but not the directory itself. How can I do it?

This command deletes the directory too:

aws s3 rm s3://static.mysite.com/html/ --recursive

I don't see the answer to this question in the manual entry for AWS S3 rm.

like image 282
Saqib Ali Avatar asked Jun 30 '17 20:06

Saqib Ali


People also ask

How do I remove all items from a bucket?

In the Bucket name list, select the option next to the name of the bucket that you want to empty, and then choose Empty.

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.

Can you delete an S3 bucket with objects in it?

For buckets without versioning enabled, you can delete all objects directly and then delete the bucket. For buckets with versioning enabled, you must delete all object versions before deleting the bucket. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.

How do I delete a folder from an AWS S3 bucket?

To delete a folder from an AWS S3 bucket, use the s3 rm command, passing it the path of the objects to be deleted along with the --recursive parameter which applies the action to all files under the specified path. Let's first run the s3 rm command in test mode to make sure the output matches the expectations.

How do I delete multiple objects in AWS S3?

To delete multiple S3 objects using a single HTTP request, you can use the AWS CLI, or an AWS SDK. To empty an S3 bucket of its objects, you can use the Amazon S3 console, AWS CLI, lifecycle configuration rule, or AWS SDK.

Can’t delete S3 bucket until it is empty?

Similar to the error you receive when you attempt to rmdir a directory with files in it, you can’t delete an S3 bucket until it is empty. Unfortunately, especially with S3-compatible implementations, the ability to trash all of the files may not be readily available from a web UI. That’s where s3cmd comes in extremely handy.

How do I delete all files in a specific folder in S3?

Let's first run the s3 rm command in test mode to make sure the output matches the expectations. The output shows that all of the files in the specified folder would get deleted. The folder also gets deleted because S3 doesn't keep empty folders around.


1 Answers

Old question, but I didn't see the answer here. If you have a use case to keep the 'folder' prefix, but delete all the files, you can use --exclude with an empty match string. I found the --exclude "." and --exclude ".." options do not prevent the folder from being deleted. Use this:

aws s3 rm s3://static.mysite.com/html/ --recursive --exclude ""
like image 81
Eric Lehto Avatar answered Oct 16 '22 09:10

Eric Lehto