In this series of tutorials, we are learning how to manage S3 buckets and files in those buckets using python. In this tutorial, we will learn how we can 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 we can use its credentials directly in python script. We have already covered this topic on 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 easily 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.
We get an exception telling us that 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 S3 bucket
We have covered how to delete files from the S3 bucket in the last tutorial. 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 run the above code again, 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.
Deleting S3 bucket using AWS CLI
Optionally 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
The above command removes all files from the bucket first and then it also 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 have learned that how to delete an S3 bucket using python and AWS CLI. Also, we have to empty the bucket first before we can delete that bucket. You can find code from this tutorial at GitHub Repo. I hope you have found this useful. See you in the next blog.