[Solved][AMP Error] The tag ‘link rel=canonical’ appears more than once in the document.

We received a sudden rise of errors in Search Console with the error:
“The tag ‘link rel=canonical’ appears more than once in the document”.
We are using the following two plugins:

This error arose because both plugins were adding a canonical tag to the AMP code. Removing one is the only solution.

As the canonical from Yoast was incorrect, we decided to remove Yoast’s canonical. Thanks to the Yoast team for the canonical documentation & wpseo_canonical filter. The following code is what we used to remove the canonical from the AMP output.

// Remove canonical from better-amp output.
add_filter( 'wpseo_canonical', 'remove_canonical_from_bamp' );
function remove_canonical_from_bamp( $canonical ) {
  if(is_better_amp()){
      return false;
  }
  return $canonical; /* Do not remove this line. */
}

The above code will work only if you are using the Better AMP plugin.
Happy Publishing!

[Update] Based on comments: please paste that function in your theme’s functions.php, or if you’re not sure how to edit functions.php, use the Code Snippets Plugin.