Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically delete a Google Apps Script time-based trigger by trigger name?

How can I programmatically delete a Google Apps Script time-based trigger by the triggered function's name, instead of triggerarray id?

I am creating a trigger and later on I want to delete the trigger by name. I tried this:

ScriptApp.deleteTrigger("myfunction");

but it does not work. I don't want to use this method:

var triggers = ScriptApp.getProjectTriggers();
    ScriptApp.deleteTrigger(triggers[0]);

Since I have more than one trigger at same time and might delete the wrong one! So how to delete trigger by its name instead of trigger array id?

function demo(){
  ScriptApp.newTrigger("myfunction")
    .timeBased()
    .everyMinutes(1)
    .create(); 
  }

  function myfunction(){
    // do some stuff here then stop the trigger by name
    ScriptApp.deleteTrigger("myfunction");
  }
}
like image 668
user1788736 Avatar asked Oct 17 '15 15:10

user1788736


2 Answers

If you loop through all the triggers you can access the handler function name with:

var triggers = ScriptApp.getProjectTriggers();
for ( var i in triggers ) {
  var funcName = triggers[i].getHandlerFunction()
}

...as shown in the docs.

like image 51
Bryan P Avatar answered Nov 27 '22 13:11

Bryan P


@code-guy

 // Deletes all triggers in the current project.
 var triggers = ScriptApp.getProjectTriggers();
 for (var i = 0; i < triggers.length; i++) {
   ScriptApp.deleteTrigger(triggers[i]);
 }

From Docs

like image 40
Free Boots Avatar answered Nov 27 '22 15:11

Free Boots