How to properly delete custom post type posts programmatically

I believe I found the problem. Since the wp_delete_post was before the redirect, WordPress tried to reload an inexistent page (and comments, since they are deleted automaticaly with the command) and that generated the error.

The new code is as such and it’s not generating errors:

        if( has_post_thumbnail( $post_id ) ) {
            $imagem_principal_id = get_post_thumbnail_id( $post_id );
            wp_delete_attachment( $imagem_principal_id , true );
        }
        if( get_post_meta( $post_id , 'token_personagem' , true ) ) {
            $token_id = get_post_meta( $post_id , 'token_personagem' , true );
            wp_delete_attachment( $token_id , true );
        }
        wp_redirect( $url );
        wp_delete_post( $post_id );
        exit;

Also, you may notice I added exit to the code. That is to follow guidelines in here.