title: Does the following google script exactly delete the messages with certain label?
body: <p>My goal is to make sure that all incoming Gmail messages from test@test.com are immediately permanently deleted.</p>
  <p>I have created a filter that gives new messages from this address the label &quot;deleteforever&quot;. Next, I have made a Google script that completely deletes all messages with the label &quot;deleteforever&quot;. To be certain that no other messages are deleted, I check an extra time whether the messages really are from test@test.com. In this way, when a thread contains messages from test@test.com and messages from another address, only the messages from test@test.com should be deleted.</p>
  <p>I plan to run it every minute. I have 3 questions:</p>
  <ol>
  <li>Does this algorithm always completely delete all new messages from test@test.com?</li>
  <li>Does this algorithm never delete a message from another sender?</li>
  <li>Will this not cost too much runtime, assuming that test@test.com does not send many emails? (I saw that there is a limit of 1 hour per day.)</li>
  </ol>
  <pre><code>function extractEmailAddress(header) {
    return header.match(/[^@&lt;\s]+@[^@\s&gt;]+/)[0];
  }
  
  function deleteForever() {
    var threads, msgs, label1, label2, sender,message,messageId;
    label1=&quot;test@test.com&quot;;
    label2=&quot;Test@test.com&quot;;
  
    threads= GmailApp.getUserLabelByName(&quot;deleteforever&quot;).getThreads();
    msgs = GmailApp.getMessagesForThreads(threads);
    for (var i = 0 ; i &lt; msgs.length; i++) {
      for (var j = 0; j &lt; msgs[i].length; j++) {
        message=msgs[i][j];
        sender = extractEmailAddress(message.getFrom());
        if (sender==label1 || sender==label2){
           messageId=message.getId();
           Gmail.Users.Messages.remove(&quot;me&quot;, messageId);
        }
      }
    }
  }
  </code></pre>
  <p>UPDATE</p>
  <p>Inspired by the comment of @TheMaster, the following strategy solves the potential runtime problem:</p>
  <p>-modify the filter in Gmail so that messages from test@test.com skip the inbox.</p>
  <p>-hide the &quot;deleteforever&quot; folder</p>
  <p>Now the script can be run every 5 minutes or with even lower frequency.</p>