Remove Delete Tab from Primary Tabs Local Tasks Menu in Drupal

Published Apr 1, 2025
Updated Apr 1, 2025
By Simon

To remove the delete tab in Drupal 10.4.X on all nodes you can use this code in a custom module. 

<?php

use Drupal\Core\Url;

/**
 * Implements hook_menu_local_tasks_alter().
 */
function custom_theme_menu_local_tasks_alter(&$data, $route_name, $route_parameters) {
  // Check if we're on a node page.
  if (strpos($route_name, 'entity.node.') === 0 && !empty($data['tabs'])) {
    foreach ($data['tabs'] as $group => &$tab_set) {
      foreach ($tab_set as $key => &$tab) {
        $url = $tab['#link']['url'] ?? NULL;
        if ($url instanceof Url && $url->getRouteName() === 'entity.node.delete_form') {
          unset($tab_set[$key]);
        }
      }
      // Re-index the array to avoid gaps.
      $tab_set = array_values($tab_set);
    }
  }
}