Skip to main content
Participating Frequently
December 3, 2025
Answered

JavaScript InDesign - Slow to delete table rows / How to delete multiple table rows at once

  • December 3, 2025
  • 3 replies
  • 331 views

Hi,

 

I wrote a function that deletes empty table rows, but for some reason it works very slowly. When 180 rows have to be deleted, it takes 21 seconds to do so. Considering I have two such tables and many data merged documents, it rather adds up.

 

I was wondering if someone knows why it is this slow. We thought that perhaps we could make it faster by deleting a range of rows at once rather than deleting rows one by one, but I don't know how to delete multiple rows at once.

 

Here is the code:

    function delRows(doc){
    // If the tables' rows' contents are 0 in length (i.e. empty), delete the rows

        var myRowsApp1 = doc.textFrames.itemByName("Appendix1").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp1.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp1[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                myRowsApp1[i].remove();
            }
        }
        
        var myRowsApp2 = doc.textFrames.itemByName("Appendix2").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp2.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp2[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length == 0){
                myRowsApp2[i].remove();
            }
        }

    }

 

Cheers,

Heidi

Correct answer Peter Kahrel

how to delete multiple rows at once

myTable.rows.itemByRange(3,6).remove();

but I don't think it'll make much difference. 

3 replies

rob day
Community Expert
Community Expert
December 3, 2025

Hi @Jurre37971467hccf , Maybe something like this? My test ran in less than 1 sec

 

//the document’s tables
var dt = app.activeDocument.stories.everyItem().tables.everyItem().getElements()
//delete empty rows from the document’s first table
delEmptyRows(dt[0].rows.everyItem().getElements())
//$.writeln($.hiresTimer * .00000001)
alert(dt[0].rows.length + " rows with content")

/**
* Deletes empty rows 
* @ param r the table’s rows 
* @ return void 
*/
function delEmptyRows(r){
    for (var i = r.length-1; i > -1; i--){
        if (!checkRow(r[i])) {
            r[i].remove()
        } 
    };   
}


/**
* Check if any of a rows' cells contain text 
* @ param r the row to check
* @ return true if any row cell has text 
*/
function checkRow(r){
    var b = false
    var c = r.cells.everyItem().getElements();
    for (var i = 0; i < c.length; i++){
        if (c[i].contents != "") {
            b = true
        }
    };   
    return b
}

 

 

Before and after:

 

Screen Shot 22.png

 

 

Screen Shot 23.png

brian_p_dts
Community Expert
Community Expert
December 3, 2025

One thing you can try is to define your row.length-1 variable outside the for loop, so the script is not having to check table length on every iteration. Maybe a slight bump in performance. Also not quite sure why you are shifting the string; perhaps some minor performance gains in your check.

Participating Frequently
December 5, 2025

Hi Brian, 

Thanks for the suggestion. I tried this after the optimization to delete multiple rows at once (which brought it back to 1.187 seconds) and defining the length variable actually slowed it down a tiny bit (1.216 seconds). The reason I do the shift is because the first column of the table contains row numbers, so if I wouldn't do this there wouldn't be any empty rows.

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 3, 2025

how to delete multiple rows at once

myTable.rows.itemByRange(3,6).remove();

but I don't think it'll make much difference. 

Participating Frequently
December 5, 2025

Thanks Peter! It actually made a world of difference, it went down to 1.2 seconds!

 

This is my new script:

    function delRows(doc){
    // If the tables' rows' contents are 0 in length (i.e. empty), delete the rows

        var myRowsApp1 = doc.textFrames.itemByName("Appendix1").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp1.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp1[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                lastRowApp1 = i+1;
                break;
            }
        }

        doc.textFrames.itemByName("Appendix1").tables[0].rows.itemByRange(lastRowApp1,myRowsApp1.length-1).remove();
        
        var myRowsApp2 = doc.textFrames.itemByName("Appendix2").tables[0].rows.everyItem().getElements();

        for(var i = myRowsApp2.length - 1; i >= 0; i--)
        {
            var rowContent = myRowsApp2[i].contents;
            rowContent.shift();
            if(rowContent.toString().replace(/,/g,"").replace(/\uFEFF/g,"").length != 0){
                lastRowApp2 = i+1;
                break;
            }
        }

        doc.textFrames.itemByName("Appendix2").tables[0].rows.itemByRange(lastRowApp2,myRowsApp2.length-1).remove();
    }