Skip to content

Instantly share code, notes, and snippets.

@brookinc
Last active November 17, 2021 16:04
Show Gist options
  • Save brookinc/713a02f5bad86a57cb0b996ccded7569 to your computer and use it in GitHub Desktop.
Save brookinc/713a02f5bad86a57cb0b996ccded7569 to your computer and use it in GitHub Desktop.
Google Apps Script that archives Gmail threads with a given label.
// ArchiveInboxThreads
//
// This Google Apps Script moves all threads containing any label in the labelsToArchive list out of the
// user's inbox, except for threads that also have a label from the labelsToPreserve list.
//
// If numInboxThreadsToProcess is set to a value greater than 0, the script will only examine that number
// of the most recent inbox threads when it runs; otherwise, it will examine every thread in the inbox.
//
// To use this script, visit https://script.google.com/home and click "New script",
// then paste this code into the text editor area and edit the "labelsToArchive" and "labelsToPreserve"
// lists below to suit your purposes.
//
// Then, to make this script run periodically, click the "Current project's triggers" button above
// and add a new trigger that runs the function below -- see details here:
// https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_manually
// Note that Google imposes usage quotas -- which can be as low as 50/day:
// https://developers.google.com/apps-script/guides/services/quotas
// ...but even so, you may be able to have your script run as often as once every 15 or even 5 minutes.
// If in doubt, you can err on the side of having it run more often, and then scale it back if you
// encounter failures due to too many invocations.
//
// When creating your trigger, you'll probably want to change the "Failure notification settings" drop-down
// to "Notify me immediately", so that you'll find out right away if something isn't working.
//
// (You also may need to disable Chrome's pop-up blocker -- on the right-hand side of the address bar --
// to allow you to authorize access to your Gmail account when you create your trigger.)
//
// References
// Google Apps Script: https://developers.google.com/apps-script/articles/tutorials
// Gmail script API: https://developers.google.com/apps-script/reference/gmail/
// Managing triggers: https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_manually
// Usage quotas: https://developers.google.com/apps-script/guides/services/quotas
var labelsToArchive = [
"label-to-archive",
"another-label-to-archive",
];
var labelsToPreserve = [
"label-to-preserve",
"another-label-to-preserve",
];
var numInboxThreadsToProcess = 25;
function archiveInboxThreads() {
// callback for determining which threads to archive
function shouldArchiveThread (thread) {
var shouldArchive = false;
var threadLabels = thread.getLabels();
for (var a = 0; a < labelsToArchive.length; a++) {
for (var tl = 0; tl < threadLabels.length; tl++) {
if (threadLabels[tl].getName() == labelsToArchive[a]) {
shouldArchive = true;
break;
}
}
}
if (!shouldArchive) {
return false;
}
for (var p = 0; p < labelsToPreserve.length; p++) {
for (var tl = 0; tl < threadLabels.length; tl++) {
if (threadLabels[tl].getName() == labelsToPreserve[p]) {
return false;
}
}
}
return true;
}
// run the callback on the specified number of inbox threads
var inboxThreads;
if (numInboxThreadsToProcess > 0) {
inboxThreads = GmailApp.getInboxThreads(0, numInboxThreadsToProcess);
} else {
inboxThreads = GmailApp.getInboxThreads();
}
for (var i = 0; i < inboxThreads.length; i++) {
if (shouldArchiveThread(inboxThreads[i])) {
GmailApp.moveThreadToArchive(inboxThreads[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment