When you iterate a collection - like pages, or selection or entities, and change that collection's contents - e.g. by deleting something - then you get issues because the collection changes as a result.
BUT if you 'freeze' the collection, but using .to_a OR perhaps you collect items, which are then deleted afterwards, after you exit using the collection... then it avoids the issue.