When a module needs to be uninstalled, Drupal adds links indicating there is content associated with the module which should be deleted first and provides a link Remove module_name entities. This works fine for most modules, however we have a complex module that needs to perform clean up, is there a way to override this link and have it invoke a custom entity deletion method?

In our case the Remove links that are automatically added to the module uninstall page, don't end up cleaning all items, and leave the site in bad state.

Possibly related to this question asked earlier "How to remove custom module provided entity instances by the time the module being uninstalled?"

1 Answer 1


If you're solely looking to alter those messages & links, you could to override the required_module_uninstall_validator service with a custom class that extends ContentUninstallValidator and alters its validate() method:

// In your ContentUninstallValidator child class
public function validate($module) {
  $reasons = parent::validate($module);
  // @TODO find/alter the message & link relevant to the module
  return $reasons;

However, you'll have to deal with the issue of searching through messages and being able to find the string relevant to the module. This could be problematic depending on how you're searching through the messages array.

A better approach might be to add new messages via a new uninstall validator service that can provide its own custom messages & links that will initiate your own custom entity cleanup code.

A good working example can be seen in the book module:

        class: Drupal\book\BookUninstallValidator
          - { name: module_install.uninstall_validator }
        arguments: ['@book.outline_storage', '@entity_type.manager', '@string_translation']
        lazy: true
    public function validate($module) {
      $reasons = [];
      if ($module == 'book') {
        if ($this
          ->hasBookOutlines()) {
          $reasons[] = $this
            ->t('To uninstall Book, delete all content that is part of a book');
      return $reasons;
  • thanks for the suggestion, will explore this to see if it will work in our case.
    – amit
    Jul 10, 2020 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.