Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bikramkawan/44806c50fe2bea703123217e6dfb9753 to your computer and use it in GitHub Desktop.
Save bikramkawan/44806c50fe2bea703123217e6dfb9753 to your computer and use it in GitHub Desktop.
Automate Gmail with Google Apps Script to archive emails from custom label.
function archieveReadMail() {
const labels = ['Support', 'Notifications'];
for (const label of labels) {
const getUserLabelByName = GmailApp.getUserLabelByName(label);
const threads = getUserLabelByName.getThreads();
const hasThreads = Array.isArray(threads) && threads.length > 0;
if (hasThreads) {
threads.forEach(thread => {
if (!thread.isUnread()) {
GmailApp.moveThreadToArchive(thread)
getUserLabelByName.removeFromThread(thread);
}
})
}
}
}
// Based on https://medium.com/@bikramkawan/86526aae2429
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment