Last active
September 14, 2022 17:45
-
-
Save kamipatel/20bfba1ac69c05eca3b9865baf86e1af to your computer and use it in GitHub Desktop.
delete s3 buckets
This file contains 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
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