Skip to content

Instantly share code, notes, and snippets.

@kamipatel
Last active September 14, 2022 17:45
Show Gist options
  • Save kamipatel/20bfba1ac69c05eca3b9865baf86e1af to your computer and use it in GitHub Desktop.
Save kamipatel/20bfba1ac69c05eca3b9865baf86e1af to your computer and use it in GitHub Desktop.
delete s3 buckets
import boto3
s3Client = boto3.client('s3')
s3 = boto3.resource('s3')
response = s3Client.list_buckets()
for bucket in response['Buckets']:
try:
if "cdp-cdp" in bucket['Name'] or "cdpawsstack" in bucket['Name']:
s3_bucket = s3.Bucket(bucket['Name'])
s3_bucket.object_versions.delete()
s3_bucket.objects.all().delete()
print(bucket['Name'])
response = s3Client.delete_bucket(Bucket=bucket['Name'])
except:
print("An exception occurred")
print("deleted buckets")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment