#!/usr/bin/env python3

import sys
import boto3
import botocore

s3 = boto3.resource('s3')
for bucket_name in sys.argv[1:]:
    bucket = s3.Bucket(bucket_name)
    print(f"Deleting all objects in bucket {bucket_name}...")
    try:
        response = bucket.objects.all().delete()
        print(f"Response: {response}")

        print("Deleting previous versions...")
        bucket.object_versions.all().delete()

        print(f"Deleting bucket {bucket_name}...")
        response = bucket.delete()
        print(f"Response: {response}")
        if str(response["ResponseMetadata"]["HTTPStatusCode"]).startswith("2"):
            print(f"Successfully deleted bucket {bucket_name}")
    except botocore.exceptions.ClientError as e:
        if e.response["Error"]["Code"] == "NoSuchBucket":
            print(e.__dict__)
            print(f"Could not find bucket {bucket_name}. Skipping")
        else:
            raise e
