Skip to content

Instantly share code, notes, and snippets.

@christianascone
Last active May 5, 2022 13:02
Show Gist options
  • Select an option

  • Save christianascone/d29f11e9080426b8179957f9ee1efe45 to your computer and use it in GitHub Desktop.

Select an option

Save christianascone/d29f11e9080426b8179957f9ee1efe45 to your computer and use it in GitHub Desktop.
Delete files older than 15 days stored on S3 bucket
BUCKETNAME=$1
PROFILE=$2
URL=$3
aws s3 ls $BUCKETNAME/ --profile $PROFILE --endpoint-url=$URL | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date --date "15 days ago" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
echo "Deleting $fileName"
aws s3 rm s3://$BUCKETNAME/$fileName --profile $PROFILE --endpoint-url=$URL
fi
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment