Skip to content

Instantly share code, notes, and snippets.

@glavk
Forked from eduardogspereira/s3_janitor.sh
Last active May 22, 2023 14:15
Show Gist options
  • Save glavk/f4e321b80f6bf222c967500e2c5d794e to your computer and use it in GitHub Desktop.
Save glavk/f4e321b80f6bf222c967500e2c5d794e to your computer and use it in GitHub Desktop.
Delete files from S3 that are older than X days.
#!/bin/bash
# You need to uncomment two lines for the script work.
# Please check the logic before use it.
main () {
#aws s3 ls $BUCKETNAME | grep -v ' PRE ' | grep -v ' 0 ' > data.txt
UNIXDAYS=$(date -d "$DAYS days ago" +%s)
while read LINE
do
FILEAGE=$(date -d "$(echo $LINE | awk '{print $1 " " $2}')" +%s)
if [ $FILEAGE -lt $UNIXDAYS ]
then
FILENAME=$(echo $LINE | rev | awk '{print $1}' | rev)
echo "removing $BUCKETNAME/$FILENAME."
#aws s3 rm s3://$BUCKETNAME/$FILENAME
fi
done < data.txt
}
BUCKETNAME=$1
if [ -z $BUCKETNAME ]
then
echo "Insert a valid bucket name."
exit 1
fi
DAYS=$2
if [ -z $DAYS ]
then
echo "You need the number of days. Files older than this will be deleted."
exit 1
fi
main $BUCKETNAME $DAYS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment