Created
August 16, 2023 10:08
-
-
Save alichherawalla/d2c19d9545033be1aebbc47471260ed5 to your computer and use it in GitHub Desktop.
Bash script to list, empty and delete s3 buckets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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