Jump to content

Delete a page and all his children via API


abmcr
 Share

Recommended Posts

public static function delete () {
		$response = [
			'data' => []
		];
		$id = wire('input')->get('id');	
		$page = wire("pages")->get($id)->children();
		foreach ($page as $child) {
		    $child->delete();
		}
		$page = wire("pages")->get($id);
		$page->delete();
		$return = true;
		$response = [
			'esito' => $return,
		];
	}

Via API i want t delete a page and his children ; the code delete only the children because after i get this error
 

 "devmessage": {
    "class": "ProcessWire\\WireException",
    "code": 0,
    "message": "Can't delete Page 1345 because it has one or more children.",

If i recall the same route (example: https://www.sampleweb.com/api/delete?id=1345)  a second time the page parent is correctly deleted

But there is a way for call once the route?
Thank you

 

Link to comment
Share on other sites

You can use RockMigrations for such tasks ? It's a very Powerful Module

 

Or just look at the docs ? 

https://processwire.com/api/ref/page/delete/

// Delete a page and recursively all of its children, grandchildren, etc. 
$item = $pages->get('/some-page/');
$item->delete(true);

It looks like the "true" is missing:

$item->delete(true);

"If set to true, then this will attempt to delete all children too."

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...