I need to implement the following code for the massive deletion of emails in a Gmail account (the code is not mine), but I can not get it to work.
The code I use is the following:
function cleanUp() {
var delayDays = 365 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("delete me");
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToTrash();
}
}
}
function archiveInbox() {
// Every thread in your Inbox that is read, older than two days, and not labeled "delete me".
var threads = GmailApp.search('label:inbox is:read older_than:2d -label:"delete me"');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
}
However, I'm throwing an error on line 8 with the following message:
TypeError: Can not call method "getThreads" of null
I hope someone can help me, thanks.