Restoring Deleted S3 Objects in Bulk with boto

S3 bucket versioning can be a lifesaver. If you accidentally delete an object, you can restore it in the S3 Management Console by deleting the Delete Marker:

Screenshot of Amazon S3 Management Console

But what if you’ve deleted more than a handful of objects? boto to the rescue. Adjust BUCKET_NAME and DELETE_DATE to suit.

#!/usr/bin/env python
import boto

BUCKET_NAME = "examplebucket"
DELETE_DATE = "2015-06-08"

bucket = boto.connect_s3().get_bucket(BUCKET_NAME)

for v in bucket.list_versions():
    if (isinstance(v, boto.s3.deletemarker.DeleteMarker) and
            v.is_latest and
            DELETE_DATE in v.last_modified):
        bucket.delete_key(v.name, version_id=v.version_id)

3 comments on “Restoring Deleted S3 Objects in Bulk with boto

Leave a Reply

Your email address will not be published. Required fields are marked *