Is your Gmail inbox filled with old messages?

I enjoy using Gmail because it's free, has a large amount of storage, and you can make scripts to k

I enjoy using Gmail because it's free, has a large amount of storage, and you can make scripts to keep your inbox tidy,  but you don't need to be a senior developer to create one. Here are some steps to create your own script to maintain your inbox.

  • Log into your Google account.

EmailScriptOne

  • Go to Google Scripts and click on Start Scripting to create a blank project.
  • Click on Untitled project and give it a name.

EmailScriptTwo

  • Copy and paste the following script in the Code.gs tab (copy over default myfunction) :
function auto_delete_mails() {  
  var label = GmailApp.getUserLabelByName("Delete Mail");   //This is the label you assign to a message in your Gmail account's inbox
  if(label == null){
    GmailApp.createLabel('Delete Mail');
  }
  else{
    var delayDays = 30 // Enter # of days before messages are moved to trash   
    var maxDate = new Date(); 
    maxDate.setDate(maxDate.getDate()-delayDays);    
    var threads = label.getThreads();  
    for (var i = 0; i < threads.length; i++) {  
      if (threads[i].getLastMessageDate()<maxDate){  
        threads[i].moveToTrash();
      } 
    } 
  }
}

  • Update the  delayDays to the number of days messages are to be removed from your inbox.
  • Save. It will create a "Delete Mail" label that you can use to assign an email for it to auto delete from your inbox.
  • Set trigger through Resources>Current Project's Triggers> Add one now to run it daily.

EmailScriptFour


EmailScriptFive

Now, you can label an email after reading it for your helpful script to keep your inbox tidy and free of old messages.

Add comment