Jan 07, 2020 in Drupal 8, Snippets

I play a lot with importing and exporting content lately. When I remove content, the created menu items need to be removed automatically. Here's a snippet on how to delete all links in a menu.

$mids = \Drupal::entityQuery('menu_link_content')
      ->condition('menu_name', 'custom-menu')
      ->execute();
$controller = \Drupal::entityTypeManager()->getStorage('menu_link_content');
$entities = $controller->loadMultiple($mids);
$controller->delete($entities);