Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
@daliborgogic

This comment has been minimized.

Copy link
Owner Author

commented Jul 18, 2019

The expires option is not implemented on Digital Ocean Spaces.

# (string) The date and time at which the object is no longer cacheable.
--expires "$(date -d '+5 minutes' --utc +'%Y-%m-%dT%H:%M:%SZ')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.