Answered
Peter KahrelCorrect answer
Community Expert
Nov 18, 2026 2:31 PM UTC
> I feel that frames [i]. parentPage refers to a cross page, not divided into left and right pages?
No, that has nothing to do with it.
The problem is that when you delete a page, all frames on the page are removed, which messes up the counter in the for-loop. That’s a general problem with deleting (or adding) objects in a for-loop.
A more straightforward approach, which gets around that problem, is to remove frames and record which page they’re on, making sure that you record each frame just once. When all frames are removed, remove the recorded pages in a second loop.
doc = app.activeDocument;
frames = app.selection[0].parentStory.textContainers;
offsets = [];
known = [];
for (i = frames.length-1; i >= 1; i--) {
page = frames[i].parentPage;
if (!known[page.documentOffset]) { // Avoid duplicates
offsets.push (page.documentOffset);
known[page.documentOffset] = true;
}
frames[i].remove();
}
for (i = 0; i < offsets.length-1; i++) {
doc.pages[offsets[i]].remove();
}

