Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3: Delete all versions of an object from versioned bucket in single call

I have searched this topic on aws docs/several websites/stackoverflow and was not able to find any answer.

I want to delete all the versions of an object in versioned s3 bucket in a single call.

  • I do not want to delete the delete marker.
  • Neither I want a single version of an object to be deleted.
  • Nor, I want to query all the versions of an object first and then send the DeleteObjectsAsync request.

Is there any way to delete all the versions of an object, whether it has delete marker or not, in a single call to AWS using S3's .net SDK ?

like image 475
Code-47 Avatar asked Sep 17 '25 06:09

Code-47


1 Answers

I did the same for one of my clients. We were using S3 to store many versions of a single file. After surfing the web pretty much, I was not able to find any method. The best approach is to just loop over the S3 file versions. Helpful steps:

  1. Get all versions of the object that you wish to delete.
  2. Loop onto these versions and delete one by one.

Note: I observed a weird behavior of S3 where after deleting all the versions of an object, I sometimes found a Delete Marker being created. Well of course if you delete the file from S3 console, you get a delete marker. But in my case, doing it though NodeJs, I found that only some times. So you need to handle that as well in your code. This is how I did it in NodeJS

        if (data.Deleted.DeleteMarker) {
          console.log('Found delete marker', data.Deleted.DeleteMarker);
          //Handle here to delete the deleteMarker
          }

Thanks, Upvote if it helped.

like image 133
Dikshit Kathuria Avatar answered Sep 19 '25 06:09

Dikshit Kathuria