Wednesday, April 17, 2013

Automatically delete old email from Multiple Gmail Labels

Do you use Gmail for personal use or Google Apps for your business email? Would you like a solution to automatically delete emails in your labels that are older than X days? This post if for you!

This post will show you how to automatically delete emails that are stored in your Gmail Labels if they are older than X number of days.
Requirements:
- Gmail as your email platform
- Labels that you're either moving email to or automatically moving them via filters
- The desire to automatically delete these emails

I am using this script to delete emails in my new gmail work account. It is mostly for the one-off emails that come in bulk, I only need to address them once and they get stored away until I clean them up. I also use it for all the junk email I receive that I have filtered to a "junk" label.

First step - Create a google spreadsheet to store information for the labels you'd like this script to check.
- Go to drive.google.com and create a new spreadsheet called "Email Management"
- In the first row list your label name (IE: my junk email), in the second row list how old you'd like the email to be in this label (IE if you want 14 days retention, list 14 here and it will delete anything older than 14 days)

Second Step - Create the script that will delete your email
- In the spreadsheet, click "Tools" > Script Editor
** if you see the following screen, select "Blank Project"
Google Apps Script'
- When the script editor opens, remove what is there and paste the following information
 /**
  * REQUIRED 
  * Set the URL of your spreadsheet here
  */
 function myfunction(){
   var spreadsheet = "URL/of/google/spreadsheet";
   readRows(spreadsheet);
 }

 /**
  * Function to delete messages over X numbers of days in a label
  * Code credit - http://www.addictivetips.com/web/set-gmail-to-auto-delete-emails-older-than-a-set-number-days/
  */
 function cleanUp(whatLabel,howOld) {  
   var delayDays = howOld // Enter # of days before messages are moved to trash   
   var maxDate = new Date(); 
   maxDate.setDate(maxDate.getDate()-delayDays);    

   var label = GmailApp.getUserLabelByName(whatLabel);  
   var threads = label.getThreads();  
   for (var i = 0; i < threads.length; i++) {  

  if (threads[i].getLastMessageDate()<maxDate){  
    Logger.log(threads[i].getLastMessageDate());
    threads[i].moveToTrash();
  } 
   } 
 }
 /**
  * Retrieves all the rows in the active spreadsheet that contain data and logs the
  * values for each row.
  * For more information on using the Spreadsheet API, see
  * https://developers.google.com/apps-script/service_spreadsheet
  */

 function readRows(spreadsheetURL) {
   var sheet = SpreadsheetApp.openByUrl(spreadsheetURL);
   var rows = sheet.getDataRange();
   var numRows = rows.getNumRows();
   var values = rows.getValues();

   for (var i = 0; i <= numRows - 1; i++) {
  var row = values[i];
  Logger.log(row[0]);
  cleanUp(row[0],row[1]);
   }
 }

- Go back to your spreadsheet and copy the URL, Change URL/of/google/spreadsheet to your newly copied URL

This script will read the spreadsheet URL you pasted in and manage the labels for the currently logged in Gmail user.

AT THIS POINT, THE SCRIPT IS READY TO GO ---- CAUTION ---- ONCE RUN, IT WILL MOVE ANY EMAILS IN THE LABEL(s) YOU SPECIFIED TO THE TRASH

If you click the save icon (and name this script), then the dropdown list labeled "Select function", select "my function". Once you have selected my function, the little "play or run" icon will become available, click it and it will ask you to authorize it's use. Make sure to click "authorize" here, then "Grant Access" on the next page.. When you are done with that, go back to the script and click the play button again, it will run one time and move the emails to trash in the labels you specified if they are in the retention period you specified. If you have done something wrong, clicking the button will pop up a little red box saying something is wrong. If that happens, check your label name and make sure it is correct. If you have it deleting a lot of mail, this could take some time....

Once you have it working correctly, now it's time to automate it.

In the scripting page, go to Resources > All your Triggers > Add a new trigger and add the following
trigger
Run => myfunction // this will run the specific function
Events - Tell it that you want it to run at specific time(s)
Set the timer frequency and time.

Once you save this, the script will run automatically when you selected it!

No comments:

Post a Comment