Skip to content

Instantly share code, notes, and snippets.

@pdswan
Created May 28, 2013 19:41
Show Gist options
  • Save pdswan/5665506 to your computer and use it in GitHub Desktop.
Save pdswan/5665506 to your computer and use it in GitHub Desktop.
Purge unread Gmail messages older than x days
/**
* Removes all unread threads matching the query
* older_than:<interval> label:purge-after-<interval>
*/
var intervals = ["1d", "7d", "1m"];
function purgeAll() {
intervals.forEach(function(interval) {
var query = constructQuery(interval);
var threads = GmailApp.search(query);
Logger.log("Found " + threads.length + " matching query: " + query);
inBatchesOf(threads, 100, function(slice) {
GmailApp.moveThreadsToTrash(slice);
})
})
function constructQuery(interval) {
var labelName = "purge-after-" + interval;
return "is:unread label:" + labelName + " older_than:" + interval;
}
function inBatchesOf(collection, batchSize, f) {
for(var i = 0; i < collection.length; i += batchSize) {
f(collection.slice(i, i + batchSize));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment