How to permanently delete versioned objects from AWS S3
Learn how to permanently deleted version objects from AWS S3.


Amazon's explanation of deleting a versioned object and the SDK documentation do not give an example of permanently deleting a versioned object. Here's how to do it.
require 'aws-sdk'
s3 = Aws::S3::Resource.new(
region: 'us-east-1',
access_key_id: ACCESS_KEY_ID,
secret_access_key: SECRET_ACCESS_KEY
)
bucket = s3.bucket('my-versioned-bucket')
bucket.objects.each do |object_summary|
o = bucket.object object_summary.key
# this is the secret: specify the version while deleting
o.delete version_id: o.version_id
end
If you don't specify the version, you get a delete marker, which you can proceed to delete infinite times and it will not go away :)

Seamus Abshere
Seamus Abshere is Faraday’s Co-founder and CTO (and serves as CISO), leading the technical vision behind the company’s consumer modeling platform. At Faraday, he focuses on building an API for consumer modeling and the infrastructure that helps customers turn first-party data into more actionable predictions. Before Faraday, Seamus was an Engineering Director at Brighter Planet. He studied Anthropology and Computer Science at Princeton University and is based in Burlington, Vermont.
Ready for easy AI?
Skip the ML struggle and focus on your downstream application. We have built-in demographic data so you can get started with just your PII.