DavidA
DavidA

Reputation: 4184

How do delete an aws s3 object version with null version id

I have an AWS s3 bucket that was originally created without versioning, and all of the objects uploaded during that time have a version id of 'null'.

Eventually versioning was turned on for the bucket.

After versioning was enabled, we objects were deleted from the bucket, creating delete markers instead of deleting the original.

I am attempting turn off versioning of the bucket, and permanently delete all of the objects that have been marked as deleted as well as delete the old versions of objects that have been modified.

However, I am running into issues due to the null version ids.

For the deleted objects, I am able to delete the delete marker, since it does have a version id. However, if I try to delete the original, not specifying a version id, since it is null, It simply creates a new delete marker (or updates the old marker). If I try to delete the delete marker which has a null version id, it replaces it with a new delete marker.

Before delete of delete marker with null version id: enter image description here

After delete of delete marker with null version id: enter image description here

The marker is still there, but the last modified date is updated. I don't seem to be able to delete the marker using the sdk, but only manually from the aws dashboard.

Similarly, when trying to delete the original version of an object that has been updated, but the original has a null version id, if I attempt to delete the original (by not specifying a version id), then it replaces the original with a delete marker (just as above), effectively deleting the object since now there is a delete marker that is newer than the updated version.

enter image description here

Does anyone know how I can safely delete the the objects where the originals have a null version id? Or delete just the original versions of objects that have a null version id?

I hope this all makes sense.

Upvotes: 0

Views: 1701

Answers (1)

Anon Coward
Anon Coward

Reputation: 10826

From the documentation on Managing delete markers:

To delete a delete marker with a NULL version ID, you must pass the NULL as the version ID in the DeleteObject request. The following figure shows how a simple DeleteObject request made without a version ID where the current version is a delete marker, removes nothing, but instead adds an additional delete marker with a unique version ID.

In other words, when dealing with this scenario, you use the string literal null to specify the object with the null version ID. (I'm not aware if it's explicitly documented, but it should be all lowercase to match what list-object-versions returns, case does seem to matter here)

For instance, with the AWS CLI:

aws s3api delete-object --bucket example-bucket --key example-object --version-id null

Or, similarly, with boto3 in Python:

import boto3
s3 = boto3.client('s3')
s3.delete_object(Bucket='example-bucket', Key='example-object', VersionId='null')

Upvotes: 3

Related Questions