Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active September 1, 2020 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daliborgogic/77ae6ac76b2bc922996c8e773699fb87 to your computer and use it in GitHub Desktop.
Save daliborgogic/77ae6ac76b2bc922996c8e773699fb87 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;
@daliborgogic
Copy link
Author

daliborgogic 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