title: Apps Script trigger function keeps on running though the trigger has been deleted
body: <p>I have created a trigger which will first execute after 5 minutes, the when the trigger function starts executing, the trigger is deleted at first place , then the required code executes, and then new trigger is created which executes in 1 minutes. Also I have a condition where in the trigger is deleted and never created.. but the trigger still continues to execute.
  I get total_uploads value as 29393, but it ended up adding nearly 100k records.</p>
  <pre><code>function onOpen() {
    SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Fetch CoA')
      .addItem('Fetch CoA', 'main')
      .addToUi();
  }
  
  function main() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var config = ss.getSheetByName(&quot;Config&quot;);
    var hostname = config.getRange(2, 2).getValue();
    var authorization = config.getRange(3, 2).getValue();
    var coaID = config.getRange(4, 2).getValue();
    var documentProperties = PropertiesService.getDocumentProperties();
    documentProperties.setProperties({
      'hostname': hostname,
      'authorization': authorization,
      'coaID': coaID,
      'offset': '0',
      'is_last_set': 0
    });
    var coaSheet = ss.getSheetByName(&quot;COA&quot;);
  
    if (coaSheet.getLastRow() &gt; 1) {
      coaSheet.deleteRows(1, coaSheet.getLastRow() - 1);
    }
    console.log(&quot;Getting CoA Validaton&quot;);
    writeCOA();
  }
  
  function writeCOA() {
    var documentProperties = PropertiesService.getDocumentProperties();
    var hostname = documentProperties.getProperty('hostname');
    var authorization = documentProperties.getProperty('authorization');
    var coaID = documentProperties.getProperty('coaID');
    var offset = parseInt(documentProperties.getProperty('offset'));
    var is_last_set = parseInt(documentProperties.getProperty('is_last_set'));
    var options = {
      &quot;method&quot;: &quot;GET&quot;,
      &quot;headers&quot;: {
        &quot;Authorization&quot;: &quot;Token token=&quot; + authorization
      }
    };
  
    var today = new Date();
  
    console.log(&quot;Writing the uploads array&quot;);
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var coaSheet = ss.getSheetByName(&quot;COA&quot;);
    coaSheet.getRange(&quot;A1:D1&quot;).setValues([[&quot;id&quot;, &quot;account_number&quot;, &quot;created_at&quot;, &quot;updated_at&quot;]]);
    var rowNum = 2;
    var uploads = [];
    var next_page = true;
    var total_uploads = &quot;...&quot;;
  
    console.log(&quot;Getting all Account numbers for CoA: &quot; + coaID);
  
    while (next_page) {
      documentProperties.setProperty('next_page', next_page);
      if (isTimeUp(today) &amp;&amp; is_last_set == 0) {
        console.log(&quot;RepeatWriteCoA triggered&quot;);
        // schedule a trigger for a different function
        ScriptApp.newTrigger(&quot;repeatWriteCoA&quot;)
          .timeBased()
          .everyMinutes(5)
          .create();
        break;
      }
      console.log(`Getting uploads ${offset} to ${offset + 500} of ${total_uploads}`);
      var platformUrl = `${hostname}/api/v3/chart_of_accounts/${coaID}/account_numbers?limit=500&amp;offset=${offset}`;
      //console.log(platformUrl);
      var response = UrlFetchApp.fetch(platformUrl, options);
      var jsonResponse = response.getContentText();
      var page_of_uploads = JSON.parse(jsonResponse);
      total_uploads = page_of_uploads['meta']['total'];
      documentProperties.setProperty('total_uploads', total_uploads);
      uploads = page_of_uploads['account_numbers'];
      try {
        offset = page_of_uploads['meta']['next']['offset'];
        documentProperties.setProperty('offset', offset);
      } catch (Exception) {
        next_page = false;
        documentProperties.setProperty('next_page', next_page);
        console.log(&quot;Exception in getting the next page offset hence stopping the loop&quot;);
        deleteTriggers();
        // For last set of records that needs to added in our spreadsheet
        console.log(&quot;Adding Last set of Records..&quot;);
        for (i = 0; i &lt; uploads.length; i++) {
          acc_number = uploads[i].account_number.replace(/[^a-zA-Z0-9\s\p{P}]/g, '-');
          coaSheet.getRange(&quot;A&quot; + rowNum + &quot;:D&quot; + rowNum).setValues([[uploads[i].id, acc_number, uploads[i].created_at, uploads[i].updated_at]]);
          rowNum += 1;
          documentProperties.setProperty('rowNum', rowNum);
        }
        console.log(&quot;End of Last set of Records..&quot;);
        is_last_set = 1;
      }
      console.log(&quot;Adding to spreadsheet&quot;);
      for (i = 0; i &lt; uploads.length; i++) {
        acc_number = uploads[i].account_number.replace(/[^a-zA-Z0-9\s\p{P}]/g, '-');
        coaSheet.getRange(&quot;A&quot; + rowNum + &quot;:D&quot; + rowNum).setValues([[uploads[i].id, acc_number, uploads[i].created_at, uploads[i].updated_at]]);
        rowNum += 1;
        documentProperties.setProperty('rowNum', rowNum);
      }
      console.log(&quot;End of adding rows&quot;);
      if (offset &gt;= total_uploads) {
        deleteTriggers();
      }
    }
    documentProperties.setProperty('is_last_set', is_last_set);
  }
  
  function repeatWriteCoA() {
    console.log(&quot;Triggered RepeatFunction... Running from Repeat function&quot;);
    // Delete trigger , so that new function wont be trigger untill this function ends   
    deleteTriggers();
    var documentProperties = PropertiesService.getDocumentProperties();
    var hostname = documentProperties.getProperty('hostname');
    var authorization = documentProperties.getProperty('authorization');
    var coaID = documentProperties.getProperty('coaID');
    var offset = parseInt(documentProperties.getProperty('offset'));
    var next_page = documentProperties.getProperty('next_page');
    var rowNum = parseInt(documentProperties.getProperty('rowNum'));
    var total_uploads = parseInt(documentProperties.getProperty('total_uploads'));
  
    var options = {
      &quot;method&quot;: &quot;GET&quot;,
      &quot;headers&quot;: {
        &quot;Authorization&quot;: &quot;Token token=&quot; + authorization
      }
    };
  
    var today = new Date();
  
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var coaSheet = ss.getSheetByName(&quot;COA&quot;);
    var is_last_set = parseInt(documentProperties.getProperty('is_last_set'));
    console.log(&quot;Getting all Account numbers for CoA: &quot; + coaID);
    while (next_page) {    
      if (is_last_set == 1) {
        console.log('last set already executed');
        break;
      }
      console.log(`Getting uploads ${offset} to ${offset + 500} of ${total_uploads}`);
      var platformUrl = `${hostname}/api/v3/chart_of_accounts/${coaID}/account_numbers?limit=500&amp;offset=${offset}`;
      var response = UrlFetchApp.fetch(platformUrl, options);
      var jsonResponse = response.getContentText();
      var page_of_uploads = JSON.parse(jsonResponse);
      total_uploads = page_of_uploads['meta']['total'];
      uploads = page_of_uploads['account_numbers'];
      try {
        offset = page_of_uploads['meta']['next']['offset'];
        documentProperties.setProperty('offset', offset);
      } catch (Exception) {
        next_page = false;
        documentProperties.setProperty('next_page', next_page);
        console.log(&quot;Exception in getting the next page offset hence stopping the loop&quot;);
        // For last set of records that needs to added in our spreadsheet
        console.log(&quot;Adding Last set of Records..&quot;);
        for (i = 0; i &lt; uploads.length; i++) {
          acc_number = uploads[i].account_number.replace(/[^a-zA-Z0-9\s\p{P}]/g, '-');
          coaSheet.getRange(&quot;A&quot; + rowNum + &quot;:D&quot; + rowNum).setValues([[uploads[i].id, acc_number, uploads[i].created_at, uploads[i].updated_at]]);
          rowNum += 1;
          documentProperties.setProperty('rowNum', rowNum);
        }
        console.log(&quot;End of Last set of Records..&quot;);
        is_last_set = 1;
      }
  
      for (i = 0; i &lt; uploads.length; i++) {
        acc_number = uploads[i].account_number.replace(/[^a-zA-Z0-9\s\p{P}]/g, '-');
        coaSheet.getRange(&quot;A&quot; + rowNum + &quot;:D&quot; + rowNum).setValues([[uploads[i].id, acc_number, uploads[i].created_at, uploads[i].updated_at]]);
        rowNum += 1;
        documentProperties.setProperty('rowNum', rowNum);
      }
      // Creating new trigger so that it will trigger this function next time ,
      // this time we are creating the trigger with 1 min
      if (offset &lt;= total_uploads &amp;&amp; isTimeUp(today) &amp;&amp; is_last_set == 0) {
        console.log(&quot;RepeatWriteCoA triggered&quot;);
        // schedule a trigger for a different function
        ScriptApp.newTrigger(&quot;repeatWriteCoA&quot;)
          .timeBased()
          .everyMinutes(1)
          .create();
        break;
      }
    }
    documentProperties.setProperty('is_last_set', is_last_set);
  }
  
  function isTimeUp(today) {
    var now = new Date();
    console.log(now.getTime() - today.getTime());
    return now.getTime() - today.getTime() &gt; 240000;
    // 240000 = 4 mins; this is the threshold limit
    // you are free to setup your own threshold limit
  }
  
  function deleteTriggers() {
    // Loop over all triggers.
    var allTriggers = ScriptApp.getProjectTriggers();
    if (allTriggers.length &gt; 0) {
      console.log(&quot;Deleting trigger....&quot;);
      for (var i = 0; i &lt; allTriggers.length; i++) {
        // If the current trigger is the correct one, delete it.
        ScriptApp.deleteTrigger(allTriggers[i]);
      }
    }
  }
  </code></pre>