Function to delete a post, it’s children and it’s grand children (half way there already)

Here’s what you can do:

  1. get all the ids of direct children
  2. get all ids of grand children
  3. combine
  4. trash

    function trash_redirection_frontend($post_id )
    {
    if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
    
        $args = array(
            'posts_per_page' => -1,
            'post_parent' => $post_id,
            'post_type' => 'bucket',
            'fields' => 'ids', // get only the ids, it's all we need
        );
    
        // get all children ids
        $children = get_posts( $args );
        $all_parents = array_merge( [ $post_id ], $children );
    
        // get all grand_children
        $args['post_parent__in'] = $children;
        unset( $args['post_parent'] ); // we're using the above array now
    
        $grand_children = get_posts( $args );
    
        // $all_posts now contains all affected ids
        $all_posts = array_merge( $all_parents, $grand_children );
    
        foreach($all_posts as $child){
            wp_trash_post( $child ); // use this unless you have good reason to query directly
        }
    
        $referer = $_SERVER['HTTP_REFERER'];
        wp_redirect( $referer );
        exit;
    }
    }