Search code examples
google-apps-scriptgmail

Unthread or permanently delete individual gmail messages, without turning off threading


I have documents coming in through E-mail from a scanner.

The scanner CANNOT change the subject. All email subjects from scanner are same "Scan to E-mail Server Job"

However, each file name is unique.

Google threads the messages, and I generally want to keep threading on.

I run a script to extract the PDF and put on drive, and then send the message to the trash.

The problem is.. future scans run the same script on the entire thread, so I end up with numerous copies of the same exact document every time the script is run.

I looked here and elsewhere this: permanently delete only one gmail message from a thread using a google script

    threads[i].moveToTrash();

EXPECTED BEHAVIOR: Don't run the script on messages in the trash. The problem is, the whole thread is labeled trash.

ACTUAL BEHAVIOR: It runs the script on the entire thread.. even the messages in the trash from same sender with same subject.

GOAL: Permanently delete messages so the script doesn't run on prior messages with same subject.

OR change subject to filename after receipt and stop threading on a message after its attachment is extracted

OR add a label that applies to a single message (not a thread) that doesn't clog up my labels/folders.

ALSO, to clarify, I can't just check for the filename being unique... because they are often renamed in the drive.

The last 2 lines below from http://www.googleappsscript.org/home/fetch-gmail-attachment-to-google-drive-using-google-apps-script are not working. The entire thread gets reprocessed... and all attachments get added again.

  var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      //get attachments
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isImageType = checkIfImage_(attachment);
        if(!isImageType) continue;
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        parentFolder.addFile(file);
        root.removeFile(file);
      }
    }
    threads[i].addLabel(label);

    //ADDED BELOW TO MOVE TO TRASH
    threads[i].moveToTrash();
    threads[i].removeFromThread();


  }

Solution

    • You want to completely delete the messages in a thread and you want to also permanently delete the thread, after all attachment files were retrieved from all messages in the thread.
    • The subject of thread is always the same.
    • You want to achieve this using Google Apps Script.

    If my understanding is correct, how about this answer? In this answer, your script in your question was modified. Please think of this as just one of several answers.

    When the thread is deleted using the method of Users.threads: delete, all messages in the thread is also deleted. This is reflected to your script.

    In this method, the thread and all messages in the thread are permanently deleted. So please be careful this when you test it.

    Modified script:

    In this modification, 3 lines were added to your script in your question. Please check the following modified script. Before you run the script, please set the variable of subject which is used for checking the subject.

    var subject = "sample subject"; // Added: Please set the subject of the thread.
    var root = DriveApp.getRootFolder();
    for(var i in threads){
      if (threads[i].getFirstMessageSubject() == subject) { // Added: Here, "subject" is checked.
        var mesgs = threads[i].getMessages();
        for(var j in mesgs){
          //get attachments
          var attachments = mesgs[j].getAttachments();
          for(var k in attachments){
            var attachment = attachments[k];
            var isImageType = checkIfImage_(attachment);
            if(!isImageType) continue;
            var attachmentBlob = attachment.copyBlob();
            var file = DriveApp.createFile(attachmentBlob);
            parentFolder.addFile(file);
            root.removeFile(file);
          }
        }
        Gmail.Users.Threads.remove("me", threads[i].getId()); // Added: Here, the thread and all mesagges in the thread are permanently deleted.
      }
    }
    

    Note:

    • Before you use this script, please enable Gmail API at Advanced Google Services.
    • From your question, I could understand that the subject of the first message of the thread is always same. Using this, when subject is the same with the subject of the first message of the thread, all attachment files are retrieved and the thread is permanently deleted.
    • In this method, the thread and all messages in the thread are permanently deleted. So I would like to recommend to test using a sample thread before you run the script for the actual email.
    • If there are several threads which have the same subject at the first message, above modified script is run for them. Also please be careful this.

    Reference: