Delete comment replies when deleting comment in WordPress

By default when you delete a comment in WordPress it does not delete replies of the comment. With this function whenever you delete a comment their replies will be automatically deleted.

add_action( 'delete_comment', function ( $comment_id ) {
    global $wpdb;
    $child = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
	
    if ( !empty($child) ) {
        foreach( $child as $child_id ) {
            wp_delete_comment( $child_id, true );
        }
    }
});

Share your thoughts by leaving a comment below.