Numbering confusion when unpublishing pages

Hello, when I want to unpublish certain pages while looping through them, the sorting numbers seem to be confused and new pages are being created.

Lets imagine I have a list of pages, all with the template page like:


and want to unpublish the second of them:

foreach( $pages as $page ){
    if( $page->slug() === 'page-two' ){

My pages now look like:

_drafts/page-two/page.txt # correct
1_page-one/page.txt # correct
2_page-three/page.txt # correct
3_page-three/default.txt # where does this come from?
3_page-four/page.txt # correct
4_page-four/default.txt # where does this come from?

Somehow two new pages were created with default template. I assume that this is the classic reindexing problem when manipulating an array while looping through it. Do I need to skip one loop? Or reindex manually? Any idea how I could solve that?

Thank you very much!

Where are you calling this code and are you calling this exactly as is?

Which Kirby version are you using?

I am running the code from a custom route so that it can be triggered by some 3rd party webhook. The example above is a little bit abstracted, but technically the same.

Kirby 3.7.3

I just tested this in a 3.7.5 Starterkit and cannot reproduce the issue. Is it reproducible somehow? How many subpages are there in your folder?

Thank you very much!
I just realized, that the problem is not caused by unpublishing the pages, but by updating some of them afterwards.

Doing a $parent->purge() solves this.