Skip to content

Instantly share code, notes, and snippets.

@n8foo
Created February 12, 2018 05:38
Show Gist options
  • Save n8foo/9947533ca1084d3c3c216e21c2ef3bdf to your computer and use it in GitHub Desktop.
Save n8foo/9947533ca1084d3c3c216e21c2ef3bdf to your computer and use it in GitHub Desktop.
var days31 = ["label1",
"label2",
"label3"];
var days7 = ["parent/label4",
"parent/label5",
"parent/label6",
"label7"];
function auto_delete_mail(userLabel,days) {
var label = GmailApp.getUserLabelByName(userLabel);
if(label == null){
GmailApp.createLabel(userLabel);
}
else{
var delayDays = days // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate){
threads[i].moveToTrash();
}
}
}
}
days31.forEach( function(s) {
auto_delete_mail(s,"31")
} );
days7.forEach( function(s) {
auto_delete_mail(s,"7")
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment