tdlm
tdlm

Reputation: 597

Delete versioned files in Amazon S3 using PHP SDK

I am tasked with cleaning up unreferenced images on our S3 bucket and found out we have versioning turned on.

We are currently using the PHP DSK (version '2006-03-01'), which, according to the documentation, does not seem to directly support deleting all versions of a particular file.

My question is this:

What is the right way to go about deleting versioned files in an S3 bucket using the methods available to me in the AWS PHP SDK?

Potentially, I was thinking of using listObjectVersions() and then running the delete on each one, but I'm not 100% that would work.

Any help is appreciated.

Upvotes: 1

Views: 696

Answers (1)

dcro
dcro

Reputation: 13649

As you mentioned, you'll need to get a list of all the object versions and figure out which ones you want to delete. Then, instead of deleting each version one-by-one you can delete multiple versions at once (up to 1000) using a single API call.

See Delete Multiple Objects and Deleting Multiple Objects Using the AWS SDK for PHP for more details.

Do not try to mix the listing & deleting operations (list 1000 versions, remove 1000, list again, remove again, ...) as you'll end up with weird results because of S3's eventual consistency model.

Upvotes: 2

Related Questions