Created
May 28, 2013 19:41
-
-
Save pdswan/5665506 to your computer and use it in GitHub Desktop.
Purge unread Gmail messages older than x days
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
/** | |
* 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