Skip to content

Delete S3 Bucket Using Python and CLI

Posted on:June 1, 2023 at 05:17 AM

In this tutorial series, we are learning how to manage S3 buckets and files in those buckets using Python. In this tutorial, we will learn how to delete S3 buckets using Python.

Setting up permissions for S3

For this tutorial to work, we will need an IAM user who has access to upload a file to S3. We can configure this user on our local machine using AWS CLI or use its credentials directly in Python script. We have already covered how to create an IAM user with S3 access. If you do not have this user setup, please follow that blog first and then continue with this blog.

Delete S3 Bucket

Boto3 provides us function “delete_bucket.” Using this function, we should be able to delete the S3 bucket quickly, correct? Let us try this.

def delete_s3_bucket():
    """
    This function deletes bucket from S3
    :return: None
    """
    s3_client = boto3.client("s3")

    bucket_name = "testbucket-frompython-2"

    response = s3_client.delete_bucket(Bucket=bucket_name)
    pprint(response)

When we run this code, we get the following output.

delete-s3-bucket-exception

We get an exception telling us that the bucket is not empty, and we will have to remove all the files in that bucket before we can delete the S3 bucket.

Deleting All Files from the S3 bucket

In the last tutorial about deleting files, We covered how to delete files from the S3 bucket. You can follow the approach mentioned in that tutorial to delete all files from the bucket.

Now that the S3 bucket is empty, let us try to delete that bucket again.

Deleting empty S3 bucket

When we rerun the above code, we should see the below output.

{'ResponseMetadata': {'HTTPHeaders': {'date': 'Mon, 20 Sep 2021 13:54:13 GMT',
                                      'server': 'AmazonS3',
                                      'x-amz-id-2': 'GuQ8dGWTEd80gZ9szx5BP/kAUZd
Te7E5247gQbYSk1OsY0F8u2fhPp4lSX76AdgfpeeKCIlOV9s=',
                                      'x-amz-request-id': '5G2Y8DWKDY71HM9V'},
                      'HTTPStatusCode': 204,
                      'HostId': 'GuQ8dGWTEd80gZ9szx5BP/kAUZdTe7E5247gQbYSk1OsY0F
8u2fhPp4lSX76AdgfpeeKCIlOV9s=',
                      'RequestId': '5G2Y8DWKDY71HM9V',
                      'RetryAttempts': 1}}

We can also validate on the S3 console that ”testbucket-frompython-2 ” has been deleted.

bucket-deleted-from-s3

Deleting S3 bucket using AWS CLI

Alternatively, we can use AWS CLI to delete all files and the bucket from the S3. All we have to do is run the below command.

$ aws s3 rb s3://bucket-name --force

deleting-all-files-from-S3-bucket

The above command removes all files from the bucket first and then removes the bucket. Please note that the above command will only work if you have not enabled versioning for your bucket.

Conclusion

In this tutorial, we learned how to delete an S3 bucket using Python and AWS CLI. Also, we must empty the bucket before we can delete that bucket. You can find code from this tutorial at GitHub Repo. I hope you have found this helpful. See you in the next blog.