Skip to content

Instantly share code, notes, and snippets.

@DaikiSuganuma
Created March 6, 2015 07:25
Show Gist options
  • Save DaikiSuganuma/e8ad208e7b5a664a59e1 to your computer and use it in GitHub Desktop.
Save DaikiSuganuma/e8ad208e7b5a664a59e1 to your computer and use it in GitHub Desktop.
function cleanUp() {
//
// Settings
//
// Enter # of days before messages are moved to trash
var days = 7;
// List of labels which you want to remove
var filters = [];
filters.push('label:notification-mailmagazine');
filters.push('label:notification-facebook');
filters.push('label:notification-google+');
filters.push('label:notification-schedule');
filters.push('label:notification-twitter');
// If it found many threads,
// the loop method will be stop at around 250.
var max = 200;
//
// Generate Search Query
//
var query = [];
query.push('(');
query.push(filters.join(' OR '));
query.push(')');
query.push('is:read');
query.push('older_than:' + days + 'd');
query = query.join(' ');
// Excute
//Logger.log(query); // for Debug
var threads = GmailApp.search(query);
if (!threads) {return false;}
for (var i = 0; i < max; i++) {
if (threads[i]) {
threads[i].moveToTrash();
// for Debug
//messages = threads[i].getMessages();
//for (var j = 0; j < messages.length; j++) {
// Logger.log(messages[j].getSubject());
//}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment