Skip to content

Instantly share code, notes, and snippets.

@rhamaa
Created June 15, 2021 08:21
Show Gist options
  • Save rhamaa/aad602ad3f2117d1172dd658df619e1e to your computer and use it in GitHub Desktop.
Save rhamaa/aad602ad3f2117d1172dd658df619e1e to your computer and use it in GitHub Desktop.
Retention delete bash script S3
# Source : https://stackoverflow.com/questions/50467698/how-to-delete-files-older-than-7-days-in-s3
aws s3 ls BUCKETNAME/ | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date --date "7 days ago" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
aws s3 rm BUCKETNAME/$fileName
fi
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment