Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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