Skip to content

Instantly share code, notes, and snippets.

@alichherawalla
Created August 16, 2023 10:08
Show Gist options
  • Select an option

  • Save alichherawalla/d2c19d9545033be1aebbc47471260ed5 to your computer and use it in GitHub Desktop.

Select an option

Save alichherawalla/d2c19d9545033be1aebbc47471260ed5 to your computer and use it in GitHub Desktop.
Bash script to list, empty and delete s3 buckets
#!/bin/bash
# List all buckets
buckets=$(aws s3api list-buckets --query 'Buckets[].Name' --output text)
for bucket in $buckets; do
echo "Processing bucket: $bucket"
# Prompt user for action
read -p "Do you want to empty and delete the bucket $bucket? (y/n): " response
if [[ $response == "y" || $response == "Y" ]]; then
# Empty the bucket
echo "Emptying bucket: $bucket"
aws s3 rm s3://$bucket/ --recursive
# Delete the bucket
echo "Deleting bucket: $bucket"
aws s3api delete-bucket --bucket $bucket
echo "Completed processing for bucket: $bucket"
else
echo "Skipping bucket: $bucket"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment