I am trying to delete an aws-s3-bucket. Before deletion of the s3 bucket, all objects inside the bucket must be deleted. AWS provides an api endpoint for this task:
https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html
It accepts keys of the object to be deleted. I do not want to specify each key manually and I am using a wildcard character(‘*’) for it.
URL used:
https://bucket-name.s3.us-east-1.amazonaws.com/?delete
I am passing following headers:
{
"Content-Type": "application/xml",
"Content-MD5": "dORwpp72d+B0KHD1GROozQ==",
"Host": "bucket-name.s3.us-east-1.amazonaws.com",
"Content-Length": 50,
"X-Amz-Security-Token": "*****",
"X-Amz-Content-Sha256": "*****",
"X-Amz-Date": "*****",
"Authorization": "*****"
}
Content-MD5 is the hash of the request body.
I performed following experiments:
- Used following request Body:
<Delete><Object><Key>*</Key></Object></Delete>
I was expecting that this will delete all objects but no objects got deleted.
Probably this happened because its considering * as a key which is not the key of any object. The
response body returned was:
<?xml version="1.0" encoding="UTF-8"?>
<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Deleted><Key>*</Key></Deleted></DeleteResult>
- Did minor changes to request body, like changing * to * which is encoded form of * but the error remains the same.
- I tested whether I can delete single objects by entering key for them, which was success.
Is there a way I can delete all objects of the s3 bucket without typing all the keys manually?
I have to perform this operation using AWS signed request.
2
Answers
Here is the AWS CLI command (use extreme caution)
S3 APIs don’t support wildcards but you can delete up to 1000 objects per API request using DeleteObjects.
Alternatively, here is a Java example of listing all objects and their versions, deleting them all, and finally deleting the bucket. Expand the ‘Using the AWS SDK for Java’ section on that page. Obviously, remove the bucket deletion if you don’t want to do that.