Archive Old Email Messages in Gmail Automatically

A
Published in: Gmail - Google Apps Script

The Google Script will archive all email threads in a specific Gmail label that have been inactive for more than a week. It takes the date of the last message in a thread and if it is older than a week, the thread is archived. The current labels is removed as well.

function ArchiveOldEmails(gmailLabelName){

  var gmailLabel = GmailApp.getUserLabelByName(gmailLabelName);

  var ONE_WEEK = 60 * 60 * 24 * 7 * 1000;

  var threads = label.getThreads();
  var now     = new Date();

  for (var j=0; j<threads.length; j++) {
    if (now - threads[j].getLastMessageDate() > ONE_WEEK){
      threads[j].moveToArchive().removeLabel(gmailLabel);
    }
  }

}
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch