Deleting a group of pages fails

I want to delete a group of pages like this. But not all the pages are removed.
Isn’t it possible to delete more than 1 page at a time?

 foreach($availabilitiespage->children() as $avpage){
 	 $avpage->delete(true);
 }

before (i have 8 pages):

34

after i run my code there’s still 1 page that’s not deleted:

52

Do these subpages all share the same blueprint? If not, is deleting maybe set to false for the one that isn’t deleted?

Yes, they all have the same blueprint.

Hm, that sounds a bit weird. Is the script running into a timeout, maybe? Try wrapping it in a try-catch block to see if you get any error messages.

No there’s no timeout.

Now i tested with 7 pages
first i did a loop without delete-action :

foreach($availabilitiespage->children() as $avpage){
 	try{
 	  echo "try " .$avpage->title() .'<br/>';
 	  // $avpage->delete(true);
 	 }
 	 catch (Exception $error) {    
 	 	echo "failed". $avpage->title(); 	 
    } 
 }

i see all my pages:
try SPEAKER
try ACT01
try ACT02
try ACT03
try BLCK01____a
try BLCK01____b
try BLCK02____c

Now when i activate the delete-action

$avpage->delete(true);

i got this result: the second in the list (ACT01) is not in the try and not in the catch, it’s skipped … I also changed the order of the items and the names, but it seems it’s always the second one that is skipped.

try SPEAKER
try ACT02
try ACT03
try BLCK01____a
try BLCK01____b
try BLCK02____c

Hm, I have no idea what could be causing this, especially since it’s not always the same page but the second in your list.

Do you see any errors in your PHP error logs, maybe?

No, i checked this and i see no errors concerning this.

It sounds like a bug. I tested this on another place with the same problem.
I just created dynamically 100pages. Then i tried to delete them and they are all deleted except 1. The second is not deleted.

when you loop through the ‘listed’ pages it’s even worse, then only the first page is deleted.

foreach($availabilitiespage->children()->listed() as $avpage){
  $avpage->delete(true);
}

Strange, I tested the code in a Starterkit without any problems, all pages gone.

Hi, yes strange indeed.

I downloaded the latest Starterkit now and i added this code to the home.php template. This code should delete the notes-subpages and the photography-subpages.

The result for me is that the photography-pages are gone except 1 (the second in the list ‘trees’ has not been deleted)
For the notes succeeded page 1, 3, 5 and 7. (note 2, 4 and 6 are not deleted).

<?php
$kirby = kirby();
$kirby->impersonate('kirby');

$notes=page('notes')->children();
foreach($notes as $note){
  echo $note->title().'<br/>';
  $note->delete(true);
}

$photography=page('photography')->children();
foreach($photography as $item){
  echo $item->title().'<br/>';
  $item->delete(true);
}
?>

Ok, I’ll do the same test now.

Edit: Yes, I got the same result now.

Should i create an issue for this?

Yes, please do.

Its strange, because it worked yesterday in my other Starterkit, but we should really look into this.

@kmajort this is a problem with deleting items from a collection while looping through it. Bastian posted a solution here: https://github.com/k-next/kirby/issues/1232

1 Like

What also works for me is using the map() method:

 $result = page('notes')->children()->listed()->map(function($item) {
    $item->delete();
    return true;
  });
1 Like

@moeli @texnixe Thanks, both workarounds work perfect!

This really feels counter-intuitive to me tbh.

Maybe add this to the docs of ->remove()-methods as extra info? I’m sure others will struggle with this too in the future.

@texnixe @moeli
I tested both codes in a hook on the Starterkit project.
The notes are deleted correctly. But the Photography-pages are not. Only the first item is deleted. Is there a solution/workaround to get this working?

'page.changeStatus:after' => function ($newPage, $oldPage) {
    if($newPage->intendedTemplate() == 'about' && $newPage->status() == 'unlisted') {
	 foreach(page('photography')->children()->data() as $item){
		 $item->delete(true);
	}
	/*$result = page('photography')->children()->listed()->map(function($item) {
		 $item->delete();
		 return true;
	});*/
   }
}

This issue should be fixed in 3.3.0.