Versioning in AWS S3 is a way to keep multiple variants of an object in the same bucket. S3 Versioning can be used to preserve, retrieve and restore every version of every object stored in your bucket.

In this article, we will look into how you can delete a versioning enabled S3 bucket.

Table of contents

Deleting an object from a versioning-enabled bucket

When you delete an object from a versioning-enabled S3 bucket, the object isn’t actually deleted. Instead, S3 places a delete marker and S3 behaves as if the underlying object was deleted.

A delete marker has a key name and version ID associated with it.

If you try to retrieve an object and it’s current version is a delete marker, S3 responds with a 404 Object Not Found error.

How to delete an S3 bucket that has versioning-enabled?

To delete the S3 bucket, we need to ensure that:

  • All objects and their versions have been deleted.
  • All delete markers for the objects have been deleted

Deleting all versioned objects

We will be using the s3api command and the delete-objects subcommand to delete all the versioned objects.

We will also need to use the list-object-versions subcommand to first retrieve all the object versions that need to be deleted.

These two commands can be chained as follows:

aws s3api delete-objects --bucket ${YOUR_BUCKET} \
--delete "$(aws s3api list-object-versions --bucket ${YOUR_BUCKET} --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}')"

Deleting all delete markers

Next, we will delete all the delete markers before we delete the bucket itself.

Similar to the previous example, we will query for the DeleteMarkers first and then call the delete operation on them.

aws s3api delete-objects --bucket ${YOUR_BUCKET} \
--delete "$(aws s3api list-object-versions --bucket ${YOUR_BUCKET} --query='{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}')"

Deleting the bucket

We will use the delete-bucket subcommand under s3api to delete the bucket.

aws s3api delete-bucket --bucket ${YOUR_BUCKET}