How Do We Automatically Delete Files Older than X Days

dreamobjects

#1

Hello. I am new to using DreamObjects and we were able to get Cyberduck working with it. So far, everybody likes it. We use it a filesharing tool to send media files to and from each other and our clients. My question is that we are looking for a way to automatically delete files within one bucket, under any folder, older than 14 days. We add a lot of files for temp storage but would like to delete them to keep our costs down. Does anybody know how we would implement this? Could we run a plugin from wordpress? or maybe a cron job on another hosting server somehow? Thanks for you help!


#2

I found a blog post that uses a bash script and s3cmd to do just that!

You can configure a cron job on your server to run daily and it will remove the files older than the number of days you set. You’ll likely need to install and configure s3cmd to work with DreamObjects on your server. Here are instructions for that.

#!/usr/bin/env bash
 
# Usage: ./deleteOld bucketname "30 days"
 
s3cmd ls s3://$1 | while read -r line;
  do
    createDate=`echo $line|awk {'print $1" "$2'}`
    createDate=`date -d"$createDate" +%s`
    olderThan=`date -d"-$2" +%s`
    if [[ $createDate -lt $olderThan ]]
      then 
        fileName=`echo $line|awk {'print $4'}`
        echo $fileName
        if [[ $fileName != "" ]]
          then
            s3cmd del "$fileName"
        fi
    fi
  done;

Then just set your cron job to run: deleteOld bucket-name “14 days”


#3

THANK YOU! I will try this!!!