30 Mar.2020

Drupal 10: Remove all menu items from the site programmatically

drupal entities

During a migration of the content and testing the functionality, we are playing a lot with adding/removing menu items for nodes.

Here's a snippet on how to delete all links in a menu.

// Need to replace 'sidebar-menu' menu name value with own name.
$mids = \Drupal::entityQuery('menu_link_content')
      ->condition('menu_name', 'sidebar-menu')
      ->execute();
$controller = \Drupal::entityTypeManager()->getStorage('menu_link_content');
$entities = $controller->loadMultiple($mids);
$controller->delete($entities);

Ruslan Piskarov

Ukraine
PHP/WEB Developer / Drupal Expert. More than 11 years of experience delivering Drupal based General Purpose solutions for different sectors such as Job Boards, Product Portfolios, Geo Coding, Real Estate solutions, E-Commerce, Classifieds, Corporate and online Magazines/Newspapers.