def delete_bucket_contents()

in source/shared/custom_resources/cfn_bucket_loader.py [0:0]


def delete_bucket_contents(bucket_name):
    """
    This function is responsible for removing all contents from the specified bucket.
    """
    client = boto3.client("s3", config=user_config)
    response = client.list_objects_v2(Bucket=bucket_name)
    for item in response.get("Contents", []):
        print(f'delete {item["Key"]}')
        client.delete_object(Bucket=bucket_name, Key=item["Key"])
    while response.get("NextContinuationToken", False):
        response = client.list_objects_v2(
            Bucket=bucket_name,
            ContinuationToken=response.get("NextContinuationToken"))
        for item in response.get("Contents", []):
            print(f'delete {item["Key"]}')
            client.delete_object(Bucket=bucket_name, Key=item["Key"])