Auto Deleting Gmail emails after 4 days

I’m working on a project that needs an external MySQL backup sent to an email address as an attachment. This is all working fine. I needed to install mutt on the server and then add this to the successful back up

echo "Hi, This is the database backup" | mutt -a /backups/mysql/a.sql.gz -s "Database backup" -- a@b.com

I needed to set the /etc/Muttrc set copy=no, otherwise I was getting an error message and the database wasn’t sent

This is going to result in the gmail inbox getting pretty full so I set up a script to delete all emails older than 4 days.

First I went to http://www.google.com/script/start/ to create a script. I created a blank project and copied the code below. I saved the project and then went to Resources -> current project’s triggers. I set the script to run every 12 hours and that meant that there were some current database backups if anything should happen to the server.

/**
* Adapted from http://www.addictivetips.com/web/set-gmail-to-auto-delete-emails-older-than-a-set-number-days/
*/
function cleanUp() {
   var delayDays = 4 // Enter # of days before messages are moved to trash
   var maxDate = new Date();
   maxDate.setDate(maxDate.getDate()-delayDays);
  //var label = GmailApp.getInboxThreads();
   var threads = GmailApp.getInboxThreads();//label.getThreads();
  for (var i = 0; i < threads.length; i++) {
    if (threads[i].getLastMessageDate()<maxDate)
   {
     threads[i].moveToTrash();
     Utilities.sleep(500);
   }
  }
}

Comments are closed.