How do I overwrite meta tags? In Drupal 7 I used this to unset unneeded elements and total overwrite meta tags:

function MYTHEME_html_head_alter(&amp;$head_elements) {
  // Unset all.
  $remove_array = array(
    'system_meta_content_type', 
    'system_meta_generator', 
    'system_shortcut_icon', 
    'metatag_canonical',
  );
  foreach ($remove_array as $key) {
    if ($head_elements[$key]) {
      unset($head_elements[$key]);
    }
  }
  // Optimize the mobile viewport.
  $head_elements['mobile_viewport'] = array(
    '#type' =&gt; 'html_tag',
    '#tag' =&gt; 'meta',
    '#weight' =&gt; -10,
    '#attributes' =&gt; array(
        'name' =&gt; 'viewport',
        'content' =&gt; 'initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width',
    ),
  );
  // ...
  // Many other my META tags
  // ...
}


But in Drupal 8 this doesn't work anymore. New elements such as $head_elements['mobile_viewport'] don't get added.

And old meta tags, such as


&lt;meta name="Generator" content="Drupal 8 (https://www.drupal.org)"&gt;
&lt;meta name="MobileOptimized" content="width"&gt;
&lt;meta name="HandheldFriendly" content="true"&gt;
&lt;link rel="shortcut icon" href="http://example.com/core/misc/favicon.ico" type="image/vnd.microsoft.icon"&gt;


are still in &lt;head&gt;. How can I remove them and add others instead in Drupal 8?
The invocation of the hook_html_head_alter was removed from Drupal core.
Issue - Remove invocation of hook_html_head_alter().

D8 has a new hook to achieve it : hook_page_attachments_alter().

Code from the change record :

function MYTHEME_page_attachments_alter(array &amp;$page) {
    $viewport = [
      '#type' =&gt; 'html_tag',
      '#tag' =&gt; 'meta',
      '#attributes' =&gt; [
        'name' =&gt; 'viewport',
        'content' =&gt; 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no',
      ],
    ];
    $page['#attached']['html_head'][] = [$viewport, 'viewport'];
  }