You have this list: (0=child1, 1=child2, 2=child3). The first time through the loop you delete entry number 0, so now you have (0=child2, 1=child3). Then you delete entry number 1, so now you have (0=child2). Finally you delete entry number 2, which doesn't exist, so you still have (0=child2).

Either you should delete the entries in reverse order (2, then 1, then 0) or you should delete entry number 0 every time through the loop.

This is a classic gotcha of programming languages, nothing to do with XML at all, and people have been getting tricked by it for over 40 years now. You aren't the first.