Skip to content

Instantly share code, notes, and snippets.

@techact
Forked from daliborgogic/awss3deleteolderthan.sh
Created September 1, 2020 04:31
Show Gist options
  • Save techact/c13c7956125f684051174b1d7652ba87 to your computer and use it in GitHub Desktop.
Save techact/c13c7956125f684051174b1d7652ba87 to your computer and use it in GitHub Desktop.
Delete files older than n in S3
#!/bin/bash
# Usage: . ./awss3deleteolderthan.sh [bucket] [path] "20 minutes ago"
endpoint="https://ams3.digitaloceanspaces.com"
aws s3 ls s3://$1/$2 --endpoint $endpoint --recursive | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date --date "$3" +%s`
if [[ $createDate -lt $olderThan ]]; then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
aws s3 rm s3://$1/$fileName --endpoint $endpoint
fi
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment