Monday, 21 January 2013

How to Auto Delete Older Email Messages in Gmail using Auto Purge

Now you can auto delete unnecessary Gmail Email messages after regular interval and keep your mailbox clean using auto purge option.

If your mailbox Trash and Spam folders are filled with so much Email and you don’t have time to delete these mail every time then you have a option using which you can  auto deleting your Email after regular time period. The option is “Auto Purge” using this option you can specify the time period and set the specific label Email messages to auto delete.


Once you activate auto purge option your Email in the Trash and Spam folders will get auto deleted after the time period that you have set. You can also specify a specific label Emails like unwanted newsletter to auto purge.

How to use Auto Purge Option in Gmail?

To enable your auto purging option you need a Google Script code this Google Script identifies the Email to delete.

Google Script Code: 

// The name of the Gmail Label that is to be purged?
var GMAIL_LABEL = "Newsletters";    


// Purge messages automatically after how many days?
var PURGE_AFTER = "10";

*/


function Intialize() {
  return;
}

function Install() {

  ScriptApp.newTrigger("purgeGmail")
           .timeBased()
           .at(new Date((new Date()).getTime() + 1000*60*2))
           .create();
  
  ScriptApp.newTrigger("purgeGmail")
           .timeBased().everyDays(1).create();

}

function Uninstall() {
  
  var triggers = ScriptApp.getScriptTriggers();
  for (var i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
  
}

function purgeGmail() {
  
  var age = new Date();  
  age.setDate(age.getDate() - PURGE_AFTER);    
  
  var purge  = Utilities.formatDate(age, Session.getTimeZone(), "yyyy-MM-dd");
  var search = "label:" + GMAIL_LABEL + " before:" + purge;
  
  try {
    
    var threads = GmailApp.search(search, 0, 100);
    
    if (threads.length == 100) {
      ScriptApp.newTrigger("purgeGmail")
               .timeBased()
               .at(new Date((new Date()).getTime() + 1000*60*10))
               .create();
    }
    
    for (var i=0; i<threads.length; i++) {
      var messages = GmailApp.getMessagesForThread(threads[i]);
      for (var j=0; j<messages.length; j++) {
        var email = messages[j];       
        if (email.getDate() < age) {
          email.moveToTrash();
        }
      }
    }
    
  } catch (e) {}
  
}


Procedure:

  • First copy the above  Google Script Code into your Google Drive.
  • Then copy that script and open Google Script and add the code in it or Select the Google Script file from your Google Drive.
  • Place the value of GMAIL_LABEL to the label that you desire to auto-purge and PURGE_AFTER are the number of days for which you want to keep a message in Gmail.
  • Run -> Initialize and give the needed permissions. This is your private script and no one will ever have access to your data.
  • Then Run -> Install to install the auto-purge script.
 Once you complete the above procedure this Google Script will continuously monitor your Gmail accounts Email and delete them after the regular time period. If you want to remove that script then simply go to Google Script account and uninstall the script.   

If you want more information then you can go to Google Script help option and get the detail information about Google Script.

No comments:

Post a Comment