Apologies if this has already been captured somewhere else. I did a search on the issue queue but couldn't see anything.
Feel free to close as a duplicate if already similar issue exists.

Problem/Motivation

After creating a menu link under any menu using module.links.menu.yml, it is not deleted even after deleting the module.

I created a simple test module to provide a single link. The only file I have in my module is test.links.menu.yml apart from test.info.yml file.

# External link.
test.drupal.org:
  title: Drupal.org
  description: Drupal is an open source platform for building amazing digital experiences.
  url: https://www.drupal.org
  menu_name: main
  weight: 40
  options:
    attributes:
      target: _blank

After installing the module, a new menu link appeared in main menu as expected.
Then I have changed the position of the menu / updated its weight and saved the changes.
changes-saved-screenshot

Then uninstalled the module, still the link persists there. I tried clearing caches but no luck.
I found that `menu_tree` table still contains the data for my custom menu.
PMA

Note: I tried clearing the cache before uninstalling the module, this case works fine.

Steps to reproduce

  1. Create a module with custom menu link
  2. Enable the module and check the link is created.
  3. Change the weight of the link.
  4. Save the changes.
  5. Uninstall the module.
  6. Check the link persists.
  7. Try clearing cache.
  8. Check again, the link persists.
  9. Check database table menu_tree, the data for link still exists.

Expected Result

The menu link should be deleted.

Actual Result

The menu link persists.

Note: If I rebuild cache before uninstall (step 5), it works fine, the link also will be deleted.

Remaining tasks

Find the bug and resolve

Issue fork drupal-3324291

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sahal_va created an issue. See original summary.

larowlan’s picture

Status: Active » Closed (works as designed)
Issue tags: +Bug Smash Initiative

The act of editing the link moves it into a menu override, if you check your config you should find it in the menu overrides config Object

If you want to remove it, you should be able to either delete the link or reimport your config from beforehand

sahal_va’s picture

Status: Closed (works as designed) » Active

Hi @larowlan,
Thanks for this info.

But I am confused why it gets deleted when I rebuild cache before module uninstall.
It shows different behaviour in following 2 cases

Case 1:

  1. Create a module with custom menu link
  2. Enable the module and check the link is created.
  3. Change the weight of the link.
  4. Save the changes.
  5. Uninstall the module.

In above case the link will not be deleted.

Case 2:

  1. Create a module with custom menu link
  2. Enable the module and check the link is created.
  3. Change the weight of the link.
  4. Save the changes.
  5. Rebuild the cache. drush cr
  6. Uninstall the module.

In above case the link will be deleted.

Can you explain why output for above two cases are different?

andypost’s picture

I think it still a bug as users can just move a module provided link (to other menu for example) and later just delete the module but menu overrides/links should not throw fatal when link/override has non-existing router

PS: I came from #3324252: User account menu not editable after uninstall

larowlan’s picture

> Can you explain why output for above two cases are different?

Uninstalling a module should clear the cache

sahal_va’s picture

@larowlan
Sorry I didn't understand your reply.
Obviously uninstalling a module will clear cache.

larowlan’s picture

Oh sorry, I misread, you cleared the cache before you deleted

larowlan’s picture

Priority: Major » Normal

I'm not sure this is major

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

eashika made their first commit to this issue’s fork.

Changes to the menu UI, such as modifying the appearance or positioning of menu items, are not considered configuration changes and are not automatically detected by Drupal. That’s why manual cache clear is needed for visible changes.
One fix could be clearing the router cache before clearing the config factory in ModuleInstaller.php’s uninstall method. Hence creating a patch.

enaznin’s picture

Status: Active » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.56 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

ameymudras made their first commit to this issue’s fork.

ameymudras’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This will need a test case showing the issue.

slogar32’s picture

i've posted an answer on Drupal Answers for a workaround, until this issue is fixed -> https://drupal.stackexchange.com/a/318397/104069

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.