title: Deleting rows in google sheets using Google Apps Script
body: <p>I encountered the weirdest error while trying to delete rows that match a specific value using Google Apps Script.</p>
  <p>Here is my Code:</p>
  <pre><code>function myFunction() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getSheetByName(&quot;file.csv&quot;);
    
    var values = sheet.getRange(&quot;N2:N&quot;).getValues();
    
    var row_del = new Array();
      
    for(var i=0;i&lt;values.length;i++)
    {
      if(values[i] == 'del'){
        row_del.push(i+2); // This line was added for debugging purposes.
       // sheet.deleteRow(i+2) was the line that was in this condition
       // (i+2) is used because row 1 has headers and the range starts from 0.
      }
    }
    
  //  Logger.log(row_del);
  //  GmailApp.sendEmail(&quot;my_email_address&quot;, &quot;subject&quot;, row_del)
   
    for (var i = 0; i &lt; row_del.length; i++)
    {
      sheet.deleteRow(row_del[i]);
    }
    
  }
  </code></pre>
  <p>The code that I have written picks up the row numbers that should be deleted but not all these rows are deleted in my first try. I should execute my script a number of times for these rows to be deleted.</p>
  <p>If my code has an error, it should show up and if the logic is wrong, incorrect rows must be deleted. I encounter neither of these scenarios and I should just execute this function multiple times.</p>
  <p>Is there something that I'm missing here?</p>