Search code examples
javascriptgoogle-apps-scriptgmailgoogle-appsgs-installable-triggers

Dynamically set google script time driven timers and too many timers error in gmail


I am using google script for gmail to create a script that will mark emails older than 150 days old as about to be deleted. Because the script will time out after x amount of time, I handle as many emails as I can before that amount of time then set a new trigger for the script to continue running where it left off in 2 minutes.

What I am worried about is that after that new trigger is set and runs it still shows up in the 'current apps triggers' even though it was a specific date and time trigger. If this happens often enough over a period, of say a month, will it cause this error:

'This script has too many triggers. Triggers must be deleted from the script before more can be added.'

Or will the expired specific date and time triggers be deleted automatically at some point?

Does anyone know of a best practice for this situation?


Solution

  • Apps script has a limited of 20 triggers per user/script. To get around this, I would recommend deleting any old triggers whenever the script is run, using the ScriptApp class. ScriptApp.getProjectTriggers() can be used to grab an array of the projects triggers. If you are only using triggers in this scenario, and deleting them everytime the script is run, your code would look something like this :

    function triggered(e){
        var triggers = ScriptApp.getProjectTriggers();
        try{
           ScriptApp.deleteTrigger(triggers[0]);
        }catch(e){
    
        }
        mainLogic()
    }