Home > Blockchain >  how can I exclude in a thread messages from the trash before they are permanently deleted in 30 days
how can I exclude in a thread messages from the trash before they are permanently deleted in 30 days

Time:06-03

a filter puts certain emails in a label then I read them with

var folder = "[Gmail]/MAJTableauLR";
  var threads = GmailApp.getUserLabelByName(folder).getThreads();

and extract data the thread is then trashed.

for (n in threads) {
        var message = threads[n].getMessages();
        message[0].moveToTrash();
    }

During a subsequent execution of the same script, it includes the messages put in the trash whereas if I put them in the trash it is good to exclude them from this folder. So how can I exclude these messages from the trash before they are permanently deleted in 30 days??

CodePudding user response:

You have two alternatives:

Deleting it permanently

Instead of moving to the trash, delete them permanently. Do you need to enable the Gmail API V1:

function main(){
  const threads = GmailApp.getUserLabelByName('Custom').getThreads()
  for(const th of threads){
    th.getMessages().forEach(msg=>deletePermanently(msg.getId()))
  }
}

function deletePermanently(msgId){
  Gmail.Users.Messages.remove('me', msgId)
}

Check the message/thread is already in the trash

You have the method for both GmailMessage and GmailThread for check if it is already trashed: isInTrash()

function main() {
  const threads = GmailApp.getUserLabelByName('Custom').getThreads()
  for (const th of threads) {
    //Will jump to the next no in trash
    if (th.isInTrash()) continue
    for (const msg of th.getMessages()) {
      // Same for msgs
      if (msg.isInTrash()) continue
    }
  }
}
  • Related