Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Somehow Drupal managed not to delete a views menu link when I removed it from Views. Any ideas how to properly delete it programmatically? I've tried something like this:
$menu_tree = \Drupal::menuTree();
$parameters = \Drupal::menuTree()->getCurrentRouteMenuTreeParameters('main');
$parameters->setTopLevelOnly();
$main_menu_top_level = $menu_tree->load('main', $parameters);
foreach ($main_menu_top_level as $item) {
// check if correct link
$item->link->deleteLink();
}
But this does not give any error nor does it delete the views menu link.
Comments
Comment #2
hkirsman CreditAttribution: hkirsman commentedComment #3
Lendude@hkirsman Did you happen to create the View and menu link in 8.0.x? In that case this would be a duplicate of #2685351: Deleting a Views Page Display doesn't remove its menu entry from a menu if it was created in 8.0.x..
Comment #4
hkirsman CreditAttribution: hkirsman commentedIt was 2-3 months ago. Pretty sure I was on 8.2.x May be the problem is the same?
Comment #5
LendudeMight be, but if you look at the other issue, no one has been able to reproduce it in more recent versions, so if you have some steps to reproduce this in more recent versions that would really help looking into this.
Comment #6
hkirsman CreditAttribution: hkirsman commentedI tried to reproduce it but could not. Before closing this could you hint how could I delete the link programmatically? Maybe somebody needs this also for future reference. It seems that views link is not entity? Is it safe to delete it directly from menu_tree table or is there some related data elsewhere?
Comment #7
LendudeI would take a look at what
\Drupal\views\Plugin\views\display\PathPluginBase::remove
does and work of that if you want to remove a link.Comment #8
pameeela CreditAttribution: pameeela commentedClosing this as a duplicate of #2685351: Deleting a Views Page Display doesn't remove its menu entry from a menu if it was created in 8.0.x. based on the comments indicating such.