Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Forked from alimbada/CleanUpGmail.gs
Last active May 18, 2024 07:17
Show Gist options
  • Save luckyshot/81607745be7d3a8a229305af3987f55d to your computer and use it in GitHub Desktop.
Save luckyshot/81607745be7d3a8a229305af3987f55d to your computer and use it in GitHub Desktop.
Google Apps Script for cleaning up Gmail
/**
* Scripts: https://script.google.com/u/0/home/my
* Triggers: https://script.google.com/u/0/home/triggers
*/
function cleanUpGmail() {
var queries = [
'from:(noreply@steampowered.com) in:inbox subject:(from your Steam wishlist on sale) older_than:7d',
'from:(no-reply@twitch.tv) in:inbox subject:(is live) older_than:1d',
'from:ebay.com subject:("is live!" OR "has been relisted") older_than:7d',
// Security alerts
'from:(no-reply@accounts.google.com) in:inbox subject:("Security alert" OR "New sign-in from") older_than:1m',
// 2FA
'from:(no-reply@email.gog.com) in:inbox older_than:1d subject:("two-step authentication" )'
]
var threads = Array();
for(var query of queries){
threads.push(...GmailApp.search(query));
}
Logger.log("Cleaning up %s threads", threads.length);
var chunkSize = 100;
for (var i = 0; i < threads.length; i += chunkSize) {
var chunk = threads.slice(i, i + chunkSize);
// Mark as read
// Logger.log("Cleaning up chunk %s", i);
// GmailApp.markThreadsRead(chunk);
// Delete
// GmailApp.moveThreadsToTrash(chunk);
// Archive
for (var i = 0; i < chunk.length; i++) {
Logger.log("Archiving chunk %s: %s", i, chunk[i].getFirstMessageSubject());
chunk[i].moveToArchive();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment