Deleting Unused Rows with Apps Script

The following function deletes all unused empty rows at the bottom of your Google Sheets sheet.


The Function

Simply copy this function and replace sheet_name with the name of the sheet in question:

function deleteEmptyRows() {
  // Source: https://www.sheets-pratique.com/en/codes/remove-unused-rows
  const sheet = SpreadsheetApp.getActive().getSheetByName('sheet_name');
  const firstEmptyRow = sheet.getLastRow() + 1;
  const rowsToDelete = sheet.getMaxRows() - firstEmptyRow + 1;
  rowsToDelete > 0 && sheet.deleteRows(firstEmptyRow, rowsToDelete);
}

Example of Deletion

By running this function on this sheet:

example sheets remove unused rows

The unused rows at the bottom of the sheet are deleted:

sheets delete unused rows remove