This site has been archived. To learn more about our current products Ibexa Content, Ibexa Experience, Ibexa Commerce head over to the Ibexa Developer Portal

eZ Community » Forums » eZ Publish 5 Platform » Delete children objects where create...

Delete children objects where create data is less than “X” hour - eZ Publish 5

Delete children objects where create data is less than “X” hour - eZ Publish 5

Monday 06 November 2017 9:03:35 am - 2 replies

Hi, I need simple php code for deleting children objects in ez publish 5, i'm trying to create cron job file and delete objects in ez publish where created date is leess then "x" hour , in my case it is 24 hour. if some have had wrote this code before, please share it me too, it would be very helpful for me.
Best regards happy.gif Emoticon

Friday 10 November 2017 1:51:11 am

Hello Nick,

In your case the code in question should be very simple to write.

You simply need to fetch the content objects which were created after the given date formula (aka last 24 hours).

We cover the concept in general here on eZpedia,

Here is a brief example:

$deleteIDArray = eZContentObjectTreeNode::subTreeByNodeID( array( 
                'Limit' => 10000,
                'Depth' => 4,
                'ClassFilterType' => 'include',
                'ClassFilterArray' => array( 'blog_post' ),
                'SortBy', array( 'published', false() ),
                'AttributeFilter', array( array( 'published', '>=', $startDateTimestamp ),
                                   array( 'published', '<', $endDateTimestamp ) )
                ), 2 );
// print_r( count( $deleteIDArray ) );
foreach ( $deleteIDArray as $deleteItem )
    $node = $deleteItem['node'];
    $nodeName = $deleteItem['node_name'];
    if ( $node === null )
        $cli->error( "\nSubtree remove Error!\nCannot find subtree '$nodeName'." );
    $nodeID = $node->attribute( 'node_id' );
    $childCount = $deleteItem['child_count'];
    $objectNodeCount = $deleteItem['object_node_count'];
    $cli->output( "Node id: $nodeID" );
    $cli->output( "Node name: $nodeName" );
    $canRemove = $deleteItem['can_remove'];
    if ( !$canRemove )
        $cli->error( "\nSubtree remove Error!\nInsufficient permissions. You do not have permissions to remove the subtree with nodeID: $nodeID\n" );
    $cli->output( "Child count: $childCount" );
    $cli->output( "Object node count: $objectNodeCount" );
    // Remove subtrees
    eZContentObjectTreeNode::removeSubtrees( array( $nodeID ), $moveToTrash );
    // We should make sure that all subitems have been removed.
    $itemInfo = eZContentObjectTreeNode::subtreeRemovalInformation( array( $nodeID ) );
    $itemTotalChildCount = $itemInfo['total_child_count'];
    $itemDeleteList = $itemInfo['delete_list'];
    if ( count( $itemDeleteList ) != 0 or ( $childCount != 0 and $itemTotalChildCount != 0 ) )
        $cli->error( "\nWARNING!\nSome subitems have not been removed.\n" );
        $cli->output( "Successfuly DONE.\n" );

This is relatively simple to do then iterate over each object with it's given ID and simply remove it from the system. The above example was cobbled together from this much older and slightly different script:

I hope this helps!


Friday 01 December 2017 10:36:02 am

Thanks  // Heath  for lost your time for me  and sorry for  my delay message.

If be honest i did it before i see your post but  i hope  it will  be helpful for others  who will have issue like me .


Best regards!


You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from