Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This looks for all mail labeled "To be archived" that is older than 1 day, archives it and removes the label.
function archiveMail() {
var delayDays = 1 // Enter # of days before messages are archived
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("To be archived");
if (label != null) {
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate) {
threads[i].moveToArchive();
threads[i].removeLabel(label);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.