google-apps-scripttestinggmail

Project Excutions say Failed even though the script seems to be working


The execution logs in Apps Script say Failed apparently even though the script seems to be working. I tested it with an email and it was eventually deleted from the Trash. I thought at first that it says Failed when it doesn't find any emails to delete but it continues to say Failed even after it appears to be working.

My Google Apps Script apparently worked in testing, then initially, in test deployment but then started failing with the following error..

Error
GoogleJsonResponseException: API call to gmail.users.messages.delete failed with error: Requested entity was not found.
deleteForever @ 
(anonymous)   @ 

This script was for my own personal use to delete certain labeled emails permanently from the trash.

function deleteForever(userId, labelName) {
  var threads = GmailApp.search("in:trash label:" + labelName);
  for (var i = 0; i < threads.length; i++) {
    Gmail.Users.Messages.remove(userId, threads[i].getId());
  }
}

deleteForever("me", "permadel")

FYI, I know very little about this scripting language or the the set up so please someone hold my hand if they know the answer.

It was weird that it apparently worked when I pressed "run" in the editor. Then, I think I selected "Test Deployment" to add it as a "developer addon" or it just added automatically and had it run every minute and it was working during testing in my Gmail.

It was only when I returned later in the day to see it failing every execution.


Solution

  • After some back and forth between the OP and me, it was found that the error was caused when the script was run by using a Google account without the Gmail service.

    The link to the user on the keyboard email inbox is done automatically. Google Apps Script knows the user on the keyboard Gmail Inbox because the user is using Google Apps Script with a Google Account. A script using Gmail or the Advanced Gmail service might succeed only if the user account has a Gmail or Google Workspace email address. It will fail when the Google account has an email address from another service as the primary email address, this is an edge case, but it was what was happening to the OP:


    Instead of making a call to the function to pass the parameter values, as it was done here:

    function deleteForever(userId, labelName) {
      var threads = GmailApp.search("in:trash label:" + labelName);
      for (var i = 0; i < threads.length; i++) {
        Gmail.Users.Messages.remove(userId, threads[i].getId());
      }
    }
    
    deleteForever("me", "permadel")
    
    

    Remove deleteForever("me", "permadel") and add default values for the function parameters in the function declaration as shown below:

    function deleteForever(userId = "me", labelName = "permadel") {
      var threads = GmailApp.search("in:trash label:" + labelName);
      for (var i = 0; i < threads.length; i++) {
        Gmail.Users.Messages.remove(userId, threads[i].getId());
      }
    }
    
    

    An alternative is to declare a test function that calls the main function with the parameter values. To avoid accidental use of the main function, make it a private function by adding an underscore as a suffix.

    function deleteForever_(userId, labelName) {
      var threads = GmailApp.search("in:trash label:" + labelName);
      for (var i = 0; i < threads.length; i++) {
        Gmail.Users.Messages.remove(userId, threads[i].getId());
      }
    }
    
    function runner(){
      deleteForever("me", "permadel")
    }
    
    

    So that you know, the code uses the Advanced Gmail Service. It should be installed to work.

    Resources


    Google Apps Script is a rapid development platform that uses JavaScript as the programming language and includes services to make it easier to automate Google apps, among other things.

    The problem is that you included a call to the function at the top level, aka global scope. When you select the function in the Google Apps Script Editor dropdown and click Run, somehow, the Google Apps Script's JavaScript engine has troubles with that at runtime.