Delete S3 Bucket Using Python and CLI

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.

When we run this code we get the following output.

delete-s3-bucket-exception
Delete S3 bucket Exception when Bucket is not empty

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.

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

bucket-deleted-from-s3
the deleted bucket is not shown in the S3 console

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.

deleting-all-files-from-S3-bucket
Deleting all files from S3 bucket using AWS CLI

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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *