Saturday, June 27, 2015

Gmail Hack - Automatically Delete Unwanted Emails

I stumbled upon this pretty useful script the other day and thought it might be useful for others to use as I am sure we all get tired of cleaning up our Gmail accounts.  The setup for this is very easy to do so let's get started.

1. Go to Script.google.com and create a blank project
2. Paste the script below into your project and save
3. Save the script however you would like.
   *Note before you save the script, feel free to change the areas below that are highlighted.  The variable of     "delayDays" is the number of days the script will look back to delete the email.  Currently I set mine to delete emails older than 15 days that have the label of "Purge" assigned to them.  You can also change the label to fit your needs appropriately.
4. Grant the script access to your Gmail account
5. Create a filter that applies the label "Purge" to whatever type of emails you want to automatically delete.
6. Setup a timer to run the script however often you would like automatically.  I have mine set to run nightly at 2am.


function cleanUp() {
var delayDays = 15 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
 
var label = GmailApp.getUserLabelByName("Purge");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
  if (threads[i].getLastMessageDate()<maxDate)
  {
    threads[i].moveToTrash();
  }
}
}

Hope you find this as useful as I did!

No comments: