Snippet: PHP Script to delete all nodes under a node in content structure

Table of contents:

This is compatible with eZ Publish 4.

About

This is a script to delete all nodes under some tree.
Please note that this script may use large amounts of memory.

A better solution has been developed called, 'batchtool'. Try using batchtool instead of this script snippet example.

Source

<?php
 
require('autoload.php');
 
function deleteNodes( $classID, $parentNodeID, $depth )
{
    global $db;
 
    $deleteIDArray = array();
    $nodeArray =& eZContentObjectTreeNode::subTree( array( 'ClassFilterType' => 'include', 'ClassFilterArray' => $classID, 'Depth' => $depth, ), $parentNodeID );
 
    print("Deleting " . count($nodeArray) . " nodes. If that is not what you want it would be good to press ctrl-c now.. deleting starts in 20 seconds!!\n\n");
     sleep(20);
 
     $deletecount = 0;
    $deleteIDArray = array();
 
     foreach ( $nodeArray as $node )
    {
        print("Deleting node " . $node->attribute( 'main_node_id' ) . ' , "' . $node->attribute( 'url_alias' ) . "'\n");
        eZContentObjectTreeNode::removeSubtrees( array( $node->attribute( 'main_node_id' ) ), false );
        $deletecount++;
        print("$deletecount, ");
    }
}
 
$cli = eZCLI::instance();
 
// change the admin-password! see below ...
 
$script = eZScript::instance( array( 'description' => ( "delete nodes ...\n\n"."delete main nodes ...,\n" ),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true,
'user' => array ( 'login' => 'admin', 'password' => 'ADMINPASS' ),
) );
 
$script->startup();
$options = $script->getOptions();
 
$script->initialize();
set_time_limit( 0 );
 
 
// 'deleteNodes' function, used to remove all nodes under passed node_id
// example usage of 'deleteNodes' function: deleteNodes( $classID, $parentNodeID, $depth )
 
deleteNodes( array( 'article' ), 6713, 10 );
 
?>