Diagnostic reference
Exception: Cannot delete all non-frozen rows
Fix Exception: Cannot delete all non-frozen rows (or columns) in Google Sheets Apps Script when calling deleteRows or deleteColumns on a sheet.
Updated December 6, 2026
Apps Script error
Exception: Cannot delete all non-frozen rows.
What it means
Google Sheets requires every tab to keep at least one unfrozen row and one unfrozen column. Calling sheet.deleteRows(1, sheet.getMaxRows()), deleting the only row below a frozen header, or deleting every remaining row in a loop throws this exception.
Related error messages
- Exception: Cannot delete all non-frozen columns.
- Exception: Cannot delete all rows in the sheet.
- Exception: Those rows are out of bounds.
Before and after
The code before and after the fix.
function resetImportTab() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Import');
if (!sheet) throw new Error('Missing Import tab.');
// Fails: Google Sheets requires every sheet to keep at least one non-frozen row.
sheet.deleteRows(1, sheet.getMaxRows());
}function resetImportTab() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Import');
if (!sheet) throw new Error('Missing Import tab.');
// Clear values while keeping frozen rows plus at least one unfrozen row.
sheet.clearContents();
const keepRows = Math.max(1, sheet.getFrozenRows() + 1);
if (sheet.getMaxRows() > keepRows) {
sheet.deleteRows(keepRows + 1, sheet.getMaxRows() - keepRows);
}
}How to fix it
- If you only want to remove data, call sheet.clearContents() (or range.clearContent()) instead of deleting rows.
- When trimming extra rows below a header, keep at least one non-frozen row: start after the frozen rows (sheet.getFrozenRows() + 1) and leave at least one row behind.
- When deleting rows in a loop, iterate from the bottom row upward and guard against deleting the last unfrozen row.
Things to check
- If row 1 is frozen (sheet.getFrozenRows() === 1) and the sheet has only 2 rows, calling deleteRow(2) also throws Cannot delete all non-frozen rows.
- myFunction flags sheet.deleteRows(1, sheet.getMaxRows()) and sheet.deleteColumns(1, sheet.getMaxColumns()) statically.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes. Writing Apps Script with an AI coding agent? Connect the Apps Script MCP server to run the same static checks before pasting code.