2 Various Ways to Delete All Rows in Excel Using Power Automate

While working on automating Excel data management, I faced the challenge of deleting all rows in an Excel table without affecting the table’s structure. After researching various approaches, I discovered two effective methods using Power Automate.

In this tutorial, I will show you how to delete all rows in Excel using Power Automate. We’ll cover two approaches: the first involves iterating through rows with the List rows present in a table and Delete a row actions. In contrast, the second leverages an Office Script executed through the Run script action to clear all rows simultaneously.

Delete All Rows in Excel Using Power Automate

Suppose you manage an “Activity Log” Excel file stored in a SharePoint document library called “Corporate Projects.” The file contains a table named “ActivityLogTable” that logs user actions. At the start of each month, you want to delete all rows in this table while keeping the table and file intact.

power automate delete all rows in excel table

Now follow the below steps:

1. In the Power Automate, click the Scheduled Cloud flow, enter the Flow name, and provide the following:

  • Starting: Provide the date you want to run your flow, like 1st December.
  • at: Provide the time you want to run the flow, like 10:00 AM.
  • Repeat every: We want to run the flow every month, so here, select 1 Month.
power automate clear excel table

2. Add the List rows present in a table action to retrieve all rows from the “ActivityLogTable” in the Excel file. Provide the below parameters:

  • Location: Select the SharePoint site address.
  • Document Library: Choose the document library.
  • File: Select the Excel file.
  • Table: Choose the Excel table value.
power automate delete rows in excel

3. Add an Apply to each loop to iterate through all the rows retrieved in the previous step.

power automate excel delete all rows

4. Inside the loop, add the Delete a row action to delete each row one by one. Provide below parameters:

  • Location: Specify the SharePoint site where the Excel file is stored.
  • Document Library: Select Corporate Projects.
  • File: Choose the Activity Log file.
  • Table: Select ActivityLogTable.
  • Key Value: Use the Key Column and Key Value fields to identify the row to delete (e.g., the row’s unique identifier or primary key).
power automate delete rows in excel table

Save the flow and test it by running it manually. Once the flow runs successfully, wait a few minutes for the changes to reflect in the Excel file. You will then see that the rows in the Excel table have been deleted.

Delete All Rows in Excel using Power Automate

Delete All Rows in Excel Using Power Automate Run script from SharePoint Library Action

To delete all rows in an Excel table using the Run Script action in Power Automate, you must create an Office Script in Excel Online and then call it through Power Automate.

Create Office Script using Excel Online

First, the office script clears all rows in the specified table (ActivityLogTable) without deleting the table structure or headers.

To create an office script in Excel, follow the below steps:

1. Go to Excel Online and open a workbook. Ensure you use a workbook stored in OneDrive or SharePoint, as Office Scripts are supported only in online workbooks. For this example, I used SharePoint:

power automate clear table

2. Click on the Automate tab in the ribbon at the top of the Excel interface (If you don’t see the Automate tab, it might need to be enabled by your organization admin). Then, click New Script. Replace the default code with the below script to delete all rows in the table:

function main(workbook: ExcelScript.Workbook) {
    let Table = workbook.getTable("ActivityLogTable");
    let RowCount = Table.getRowCount();
    Table.deleteRowsAt(0, RowCount);
}
  • getTable(“ActivityLogTable”): This retrieves the table named “ActivityLogTable” from the workbook.
  • getRowCount(): This gets the total number of rows currently in the table.
  • deleteRowsAt(0, RowCount): This deletes all rows in the table, starting from the first row (index 0) and spanning the total row count (RowCount).
power automate for each row in excel

3. After that, rename the script and save the script.

office script delete all rows in table

Create a Flow in Power Automate

Now follow the below steps to delete all rows:

1. In the Power Automate, click the Instant Cloud flow, enter the Flow name, and choose the trigger flow (i.e., manually trigger a flow).

how to delete table in excel using Power Automate

2. Add the Run script from SharePoint library action from the Excel Online (Business) connector and provide the below parameters:

  • Group: Select the group or site where your workbook resides (e.g., Power Automate).
  • Workbook Library: Choose the library where the file is stored (e.g., Corporate Projects).
  • Workbook: Enter the file name, including the path if necessary (e.g., /Activity Log.xlsx).
  • Group: Select the group or site where your script resides (e.g., Power Automate).
  • Script Library: Select the library where the script is saved (e.g., Corporate Projects).
  • Script: Choose the script you created earlier (e.g., DeleteAllRows).
Power Automate delete all cells below a row excel

Now, it’s time to save the flow. Click Test and select Manually. After it runs successfully, go to the SharePoint document library, open the Excel file, and you will see that all rows have been successfully deleted.

power automate delete a row in excel

Conclusion

In this tutorial, we explored two methods to delete all rows in an Excel table using Power Automate. The first method involves using the List rows present in a table action with an Apply to each loop and the Delete a row action to remove rows one by one. The second method utilizes the Run script action to execute an Office Script, which deletes all rows in the table in a single operation.

You may also like:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…