Last active
July 31, 2022 13:04
-
-
Save jackburns/b67db97df52dd2c0e2184391789ce9f1 to your computer and use it in GitHub Desktop.
gmail autoarchive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gmailAutoarchive() { | |
const rules = [ | |
{label: "autoarchive", olderThanDays: 3}, | |
{label: "autoarchive-delayed", olderThanDays: 7}, | |
{label: "autoarchive-read", olderThanDays: 1, onlyRead: true}, | |
{olderThanDays: 14, onlyRead: true} | |
] | |
const batch_size = 100; | |
for (let rule of rules) { | |
const { label, olderThanDays, onlyRead } = rule; | |
Logger.log(`Archiving ${onlyRead ? 'read' : 'all'} threads in ${label} older than ${olderThanDays} Days`); | |
let searchString = `in:inbox older_than:${olderThanDays}d` | |
if (label) { | |
searchString += ` label:${label}` | |
} | |
if (onlyRead) { | |
searchString += " is:read" | |
} | |
const threads = GmailApp.search(searchString); | |
Logger.log(`Identified ${threads.length} threads for archival in ${label}`); | |
while (threads.length) { | |
const currentBatch = threads.splice(0, batch_size); | |
Logger.log(`Archiving ${currentBatch.length} threads`) | |
GmailApp.moveThreadsToArchive(currentBatch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment