I’d put the dequeue statements inside the ‘if’, replacing the $loadscripts line. No need to set the flag and then check the flag to dequeue. That might simplify the code for further debugging.
Edited: suggested code corrections:
function contactform_dequeue_scripts() {
if (is_singular()) {
$post = get_post();
if (has_shortcode($post->post_content, 'contact-form-7')) {
wp_dequeue_script('contact-form-7');
wp_dequeue_script('google-recaptcha');
wp_dequeue_style('contact-form-7');
}
}
}
add_action('wp_enqueue_scripts', 'contactform_dequeue_scripts', 99);
This only dequeue’s scripts if there is a shortcode for CF7, and if it is a singular page. Otherwise, things are done normally. Easier to read the code and figure out what is happening.