Issue deleting S3 Bucket via CloudFormation

0

Hello everyone,

I’m trying to delete an S3 bucket through CloudFormation, but I receive the following error: “The bucket you tried to delete is not empty. You must delete all versions in the bucket.”

Before attempting to delete the bucket, I deleted all the objects stored in it.

What can I do to resolve this issue?

Thank you in advance for your help.

Regards

asked 20 days ago56 views
3 Answers
0
Accepted Answer

Hello.

As stated in the re:Post Agent answer, you probably have versioning enabled on your S3 bucket and old versions are still there.
There is an "Empty" button on the S3 bucket screen, so try using it to delete all the contents of the bucket.
Note that using this option will delete all objects, so be careful.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/empty-bucket.html
a

profile picture
EXPERT
answered 20 days ago
0

The issue you're experiencing is a common one when trying to delete S3 buckets through CloudFormation, especially if the bucket has versioning enabled. Here are some steps you can take to resolve this:

  1. Ensure all object versions are deleted: If your bucket has versioning enabled, deleting objects doesn't remove their previous versions. You need to delete all versions of all objects before the bucket can be deleted.

  2. Empty the bucket before stack deletion: CloudFormation doesn't automatically delete non-empty S3 buckets as a safeguard against accidental data loss. Before deleting the stack, make sure to empty the bucket completely, including all versions of all objects.

  3. Use a lifecycle rule: If your bucket contains a large number of objects and versions, it might be more efficient to set up a lifecycle rule to delete all objects and versions automatically.

  4. Retain the bucket: If you can't empty the bucket for some reason, you can use the 'RetainResources' parameter when deleting the stack. This will delete the stack but leave the S3 bucket in place, allowing you to manage it separately.

  5. Force delete: As a last resort, you can use the 'FORCE_DELETE_STACK' option with the 'DeletionMode' parameter when deleting the stack. This will attempt to delete all resources in the stack, including non-empty S3 buckets.

  6. Check permissions: Ensure you have the necessary IAM permissions to delete both the CloudFormation stack and the S3 bucket and its contents.

If you're dealing with a versioned bucket and need to delete all versions programmatically, you might need to use a script or AWS CLI commands to list and delete all object versions before attempting to delete the bucket through CloudFormation.

Remember, it's crucial to be certain you want to delete all data in the bucket before proceeding with these steps, as the deletion of object versions is irreversible.
Sources
S3 Bucket Not Deleted When Deleting CloudFormation Stack | AWS re:Post
Troubleshooting CloudFormation - AWS CloudFormation
Versioned Bucket Delete All Objects and bucket | AWS re:Post

profile picture
answered 20 days ago
AWS
SUPPORT ENGINEER
reviewed 20 days ago
profile picture
EXPERT
reviewed 20 days ago
0

As mentioned in the Document [1], You can only delete empty buckets. Deletion fails for buckets that have contents, including versions. Please refer the article [2] which has detailed explanation to delete S3 buckets. If you would like, you can also retain S3 bucket during deletion of stack or force delete stack which will retain S3 bucket automatically, please refer [3]

REFERENCES:

[1] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-s3-bucket.html

[2] https://repost.aws/knowledge-center/s3-delete-bucket

[3] https://repost.aws/knowledge-center/cloudformation-stack-delete-failed https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html

AWS
SUPPORT ENGINEER
answered 20 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions