Skip to content

Instantly share code, notes, and snippets.

@JProffitt71
Created February 17, 2014 04:29
Show Gist options
  • Save JProffitt71/9044744 to your computer and use it in GitHub Desktop.
Save JProffitt71/9044744 to your computer and use it in GitHub Desktop.
Uses s3cmd to manually remove files older than specified date (##d|m|y) in a specified bucket
#!/bin/bash
# Usage: ./s3cmdclearfiles "bucketname" "30d"
s3cmd ls s3://$1 | grep " DIR " -v | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -j -f "%Y-%m-%d %H:%M" "$createDate" +%s`
olderThan=`date -j -v-$2 +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
printf 'Deleting "%s"\n' $fileName
s3cmd del "$fileName"
fi
fi
done;
@ajdevtechgithub
Copy link

Hey Team,

I want to do same thing my s3 is on wasabi not on AWS. i want to delete 60 days older data on path like "bucketnameA/B/C/ files * " but it is not working good i have error in timestamp unix and wasabi time last modified time.

@eazylaykzy
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment