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.
In the Bucket name list, select the option next to the name of the bucket that you want to empty, and then choose Empty.
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.
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.
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.
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.
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.
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.
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 ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With