Updating slug duplicates page?

As discussed here, I’ve been trying to programatically change pages slugs, to clean them from unnecessary trailing id numbers.

But after doing so, instead of pages with the new slugs, I have duplicated pages, one with the old slug and one with the new slug. I must have done something stupid, see:

image

image

This is my code:

foreach(page('exhibitions')->children() as $e) {
    $kirby->impersonate('kirby'); 

    // Clean the slug of trailing ids
    $clean = preg_split('{(-[0-9]*)$}', $e->slug())[0];

    // If there are more than one exhibition with the same clean slug, just leave them alone, we'll deal manually with those
    if (page('exhibitions')->children()->search($clean, 'id') &&  page('exhibitions')->children()->search($clean, 'id')->count() > 1) {
        dump(page('exhibitions')->children()->search($clean, 'id'));
    // ...if actual slug and clean slug are not equal, then proceed with the slug change
    } elseif ($e->slug() != $clean) {
        try {
            $e->changeSlug($clean);
            echo 'done' . "<br />";
        } catch (Exception $e) {
            dump($e);
        }
    } else {
        echo 'nothing to change' . '<br />';
    };

What am I missing ?

/edit

Funnily enough, I just noticed all old slug pages share the same sorting number: 1!

Now that is puzzling to me.

Thanks

Was it like that before you started the renaming? Might be the issue?

Well, the backups say yes, you are correct. How did this happen, I can’t even fathom.

So in the backups there are 140 pages that start with 1_ and then 140 pages that go from 2_ to 140_. First and second group are identical except for leading num.

Funnily enough, in the backups, slugs are also identical between both groups, and the panel seems oblivious to the duplication, only showing one of each page in pages fields. Updating that page’s content updates only the page that does NOT start with 1_

So the 1_ pages seem to be in limbo.

Now, I guess I could just delete all the 1_, but then I’d have my pages start at 2_ , which seems like it could lead to further difficulties?

Is there a way to reset folder nums while keeping their actual order ?

How did I end up here? Food for thought.

Thank you

Hm, what you could do is first remove all the page that start with 1.

Then you write a PHP or bash script, that renames all folder names

Ok, well, let’s try !

Thank you