title: Cypress - delete all rows from table
body: <p>I am writing a set of tests on a list of elements (HTML table).</p>
  <p>Since the tests are still a work in progress, I'm getting a lot of junk data which I'd like to cleanup before each run.</p>
  <p>So, I'm trying to delete all rows in <code>before()</code> hook. I imagine the process for deleting should be:</p>
  <ol>
  <li>Search products by name</li>
  <li>Click a delete icon in first row</li>
  <li>Wait for confirmation dialog</li>
  <li>Click &quot;Yes&quot;</li>
  <li>Repeat from step <code>2</code> until there are no rows left</li>
  </ol>
  <p>This is the function I've got so far that I call after typing in the search input:</p>
  <pre><code> function deleteAllFoundProducts() {
          cy.get('tbody tr').then(rows =&gt; {
              if (rows.length &lt; 1) return // if no rows remain, terminate recursion
              cy.get('span[title=&quot;Delete&quot;]', {timeout: 10000}).first().click()
              cy.get('.tBtn[title=&quot;Yes&quot;]').click().then(() =&gt; {
                  cy.get('.loader', {timeout: 10000}).should('not.exist') // wait for list to reappear
                  deleteAllFoundProducts() // try again
              })
          })
      }
  </code></pre>
  <p>The problem is – the function continues to run even after deleting the final row, and errors out because there is no <code>span[title=&quot;Delete&quot;]</code>.</p>
  <p><strong>[EDIT]</strong></p>
  <p>On closer inspection I realized that an empty table still has one row – with <em>No records found</em> text. I thought it wasn't a row at all. So, the error is caused by having a row that doesn't contain a delete icon. I should have noticed that sooner.</p>
  <p>This is relevant HTML with unnecessary Angular stuff removed:</p>
  <pre><code>&lt;tbody class=&quot;p-element p-datatable-tbody&quot; ng-reflect-value=&quot;&quot; ng-reflect-template=&quot;[object Object]&quot;&gt;
      &lt;tr&gt;
          &lt;td colspan=&quot;6&quot;&gt;
              &lt;my-loader ng-reflect-loading=&quot;false&quot;&gt;
              &lt;/my-loader&gt;
              &lt;div class=&quot;p-2&quot;&gt;No matching records found. &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
  </code></pre>
  <p>The solution would, I guess, be to check if the row contains an element with text 'No records found', and perform delete only if it doesn't. But I'm not sure how to do it. Any help would still be appreciated.</p>