Automatically delete old items from s3 bucket

Solution 1:

Amazon has meanwhile introduced S3 lifecycles (see the introductory blog post Amazon S3 - Object Expiration), where you can specify a maximum age in days for objects in a bucket - see Object Expiration for details on its usage via the S3 API or the AWS Management Console.

Solution 2:

Amazon now has the ability to set bucket policies to automatically expire content:

http://docs.amazonwebservices.com/AmazonS3/latest/UG/ObjectExpiration.html


Solution 3:

You can use s3cmd to write a script to run through your bucket and delete files based on a precondition.

You'll need to write some code (bash, python) on top of it.

You can download s3cmd from http://s3tools.org/s3cmd


Solution 4:

shell script to delete old buckets using s3cmd utility
source : http://shout.setfive.com/2011/12/05/deleting-files-older-than-specified-time-with-s3cmd-and-bash/

#!/bin/bash
# Usage: ./deleteOld "bucketname" "30 days"
s3cmd ls s3://$1 | while read -r line;  do

createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-$2" +%s`
if [[ $createDate -lt $olderThan ]]
  then 
    fileName=`echo $line|awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//'`
    echo $fileName
    if [[ $fileName != "" ]]
      then
        s3cmd del "$fileName"
    fi
fi
done;

Tags:

Amazon S3