Skip to content

Instantly share code, notes, and snippets.

@tomharvey
Last active June 4, 2022 19:19
Show Gist options
  • Save tomharvey/517058b5f253909e5cab58857beebed5 to your computer and use it in GitHub Desktop.
Save tomharvey/517058b5f253909e5cab58857beebed5 to your computer and use it in GitHub Desktop.
import boto3
s3_client = boto3.client('s3')
def is_bucket_empty(bucket_name):
try:
response = s3_client.list_objects(
Bucket=bucket_name,
MaxKeys=1,
)
except:
print(f'{bucket_name} is not found?')
return False
objects = response.get('Contents', [])
return len(objects) == 0
if __name__ == "__main__":
response = s3_client.list_buckets()
bucket_list = list(response['Buckets'].copy())
print(f'{len(bucket_list)} Buckets')
for bucket in bucket_list:
bucket_name = bucket['Name']
if is_bucket_empty(bucket_name):
print(f'Deleting bucket {bucket_name}')
# response = s3_client.delete_bucket(
# Bucket=bucket_name,
# )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment