Search code examples
amazon-web-servicesamazon-s3aws-cli

How to delete 1 month old and older files in s3 using aws-cli?


My bucket has already a lot of files and I want to delete files which are 1 month old and older. I would like o delete files with out setting a Object Expiration.

Is there any way to do this using aws-cli? Thanks :)


Solution

  • Found a blog post that contains a script that should take care of your needs.

    More versatile version can be found here or written down below.

    #!/bin/bash
    
    # Usage: ./s3cmdclearfiles "bucketname" "30d"
     
    s3cmd ls s3://$1 | grep " DIR " -v | while read -r line;
      do
        createDate=`echo $line|awk {'print $1" "$2'}`
        createDate=`date -j -f "%Y-%m-%d %H:%M" "$createDate" +%s`
        olderThan=`date -j -v-$2 +%s`
        if [[ $createDate -lt $olderThan ]]
          then
            fileName=`echo $line|awk {'print $4'}`
            if [[ $fileName != "" ]]
              then
                printf 'Deleting "%s"\n' $fileName
                s3cmd del "$fileName"
            fi
        fi
      done;