Skip to content

Instantly share code, notes, and snippets.

@spullara
Created May 2, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spullara/6015d23a2e028a15355d7de5254800df to your computer and use it in GitHub Desktop.
Save spullara/6015d23a2e028a15355d7de5254800df to your computer and use it in GitHub Desktop.
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