Skip to content

Instantly share code, notes, and snippets.

@alfg
Created December 6, 2012 07:32
Show Gist options
  • Save alfg/4222529 to your computer and use it in GitHub Desktop.
Save alfg/4222529 to your computer and use it in GitHub Desktop.
Purges old objects stored in S3 based on last_modified date.
#!/usr/bin/env python
""" clean_old_s3_files.py - Purges old files based on date given
Author: Alfred Gutierrez (alfg.co)
Requires: boto
Usage: Configure Date, AWS and Bucket variables below. Then run as 'python clean_old_s3_files.py'
Disclaimer: Use at your own risk. I wrote this in 10 minutes.
"""
import boto
import datetime
YEAR, MONTH, DAY = 2012, 10, 31
AWS_KEY, AWS_SECRET = 'accesskey', 'secretkey'
BUCKET = 'bucketname'
conn = boto.connect_s3(AWS_KEY, AWS_SECRET)
rs = conn.get_all_buckets()
b = conn.get_bucket(BUCKET)
for item in b:
if item.last_modified < datetime.date(YEAR, MONTH, DAY).isoformat():
print "Deleting ", item.name
item.delete()
print "No remaining files to delete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment