# Numbering confusion when unpublishing pages

**URL:** https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445
**Category:** Questions
**Tags:** v3
**Created:** [September 9, 2022, 2:07pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445 "2022-09-09T14:07:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![moritzebeling](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/moritzebeling/32/5429_2.png) [@moritzebeling](https://forum.getkirby.com/u/moritzebeling)
#### Post date: [September 9, 2022, 2:07pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445/1 "2022-09-09T14:07:40Z")

</div>

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:

```bash
1_page-one/page.txt
2_page-two/page.txt
3_page-three/page.txt
4_page-four/page.txt

```

and want to unpublish the second of them:

```php
foreach( $pages as $page ){
    if( $page->slug() === 'page-two' ){
        $page->unpublish();
    }
}

```

My pages now look like:

```bash
_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!

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [September 9, 2022, 2:43pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445/2 "2022-09-09T14:43:07Z")

</div>

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

Which Kirby version are you using?

---

<div class="post-metadata">

### Author: ![moritzebeling](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/moritzebeling/32/5429_2.png) [@moritzebeling](https://forum.getkirby.com/u/moritzebeling)
#### Post date: [September 9, 2022, 3:02pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445/3 "2022-09-09T15:02:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [September 9, 2022, 3:27pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445/4 "2022-09-09T15:27:15Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![moritzebeling](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/moritzebeling/32/5429_2.png) [@moritzebeling](https://forum.getkirby.com/u/moritzebeling)
#### Post date: [September 9, 2022, 4:42pm UTC](https://forum.getkirby.com/t/numbering-confusion-when-unpublishing-pages/26445/5 "2022-09-09T16:42:22Z")

</div>

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.
