When we put some objects into S3 bucket to verify or validate a CloudFormation stack by uploading a template to S3 bucket, the buckets store thousands of objects that I don't have to keep anymore and we sometimes have to pay too much cost for that after receiving the billing.
Why don't we delete all of the objects and buckets simultaneously or you should consider of using
Object Lifecycle Management in advance to automatically delete the objects that has been expired.
* List the buckets you want to remove and input them into a file
$ aws s3api list-buckets | jq -r '.Buckets[] | .Name' | egrep 'bucket-name-[1-3]' | tee bucket.txt
bucket-name-1
bucket-name-2
bucket-name-3
* Recursively delete all of the objects, the buckets and confirm that they are deleted
$ while read line ; do
echo "`date '+%x %T'` ## Removing $line"
aws s3 rm s3://$line --recursive
aws s3api delete-bucket --bucket $line
aws s3 ls s3://$line
echo -e "`date '+%x %T'` ## Finished removing $line\n"
done < bucket.txt
06/11/2014 17:22:33 ## Removing bucket-name-1
delete: s3://bucket-name-1/object_01.txt
...
delete: s3://bucket-name-1/object_10.txt
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:22:39 ## Finished removing bucket-name-1
06/11/2014 17:22:39 ## Removing bucket-name-2
delete: s3://bucket-name-2/object_01.txt
...
delete: s3://bucket-name-2/object_10.txt
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:22:44 ## Finished removing bucket-name-2
06/11/2014 17:22:44 ## Removing bucket-name-3
delete: s3://bucket-name-3/object_01.txt
...
delete: s3://bucket-name-3/object_10.txt
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:22:50 ## Finished removing bucket-name-3
* Use --quiet option if you want to quietly remove
$ while read line ; do
echo "`date '+%x %T'` ## Removing $line"
aws s3 rm s3://$line --recursive --quiet
aws s3api delete-bucket --bucket $line
aws s3 ls s3://$line
echo -e "`date '+%x %T'` ## Finished removing $line\n"
done < bucket.txt
06/11/2014 17:25:56 ## Removing bucket-name-1
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:26:01 ## Finished removing bucket-name-1
06/11/2014 17:26:01 ## Removing bucket-name-2
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:26:07 ## Finished removing bucket-name-2
06/11/2014 17:26:07 ## Removing bucket-name-3
A client error (NoSuchBucket) occurred: The specified bucket does not exist
06/11/2014 17:26:12 ## Finished removing bucket-name-3
That's it!