Archive Old Email Messages in Gmail Automatically
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);
}
}
}
Amit Agarwal
Google Developer Expert, Google Cloud Champion
Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory