Delete comment replies when deleting comment in WordPress

By default when a comment is deleted, WordPress does not delete the replies of that comment. With this function whenever comment is deleted, its replies will be automatically deleted.

/**
 * Delete comment replies when deleting comment.
 */
add_action( 'delete_comment', function( $comment_id ) {
    $child = get_comments([
        'fields' => 'ids',
        'parent' => $comment_id,
    ]);
    
    if ( $child ) {
        foreach( $child as $child_id ) {
            wp_delete_comment( $child_id, true );
        }
    }
});

Share your thoughts by leaving a comment below.