I have a module that creates a custom entity. In the module.install file, in hook_uninstall, I remove any entity records.

Running drush pmu results in an error because there are entity records. It turns out that the hook_uninstall, which deletes the records along with performing other cleanup, is not firing before the error is thrown.

Drupal\Core\Extension\ModuleUninstallValidatorException: The following reasons prevent the modules from being uninstalled: There[error]
is content for the entity type: My Entities. <a href="/admin/modules/uninstall/entity/my_module">Remove my_entities
entities</a>. in /var/www/web/core/lib/Drupal/Core/Extension/ModuleInstaller.php:437
Stack trace:
#0 /var/www/web/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php(91):
Drupal\Core\Extension\ModuleInstaller->uninstall(Array, true)
#1 phar:///usr/local/bin/drush8/commands/core/drupal/environment_common.inc(187):
#2 phar:///usr/local/bin/drush8/commands/core/drupal/pm.inc(79): drush_module_uninstall(Array)
#3 phar:///usr/local/bin/drush8/commands/pm/pm.drush.inc(1265): _drush_pm_uninstall(Array)
#4 phar:///usr/local/bin/drush8/includes/command.inc(422): drush_pm_uninstall(my_module')
#5 phar:///usr/local/bin/drush8/includes/command.inc(231): _drush_invoke_hooks(Array, Array)
#6 phar:///usr/local/bin/drush8/includes/command.inc(199): drush_command('my_module')
#7 phar:///usr/local/bin/drush8/lib/Drush/Boot/BaseBoot.php(67): drush_dispatch(Array)
#8 phar:///usr/local/bin/drush8/includes/preflight.inc(67): Drush\Boot\BaseBoot->bootstrap_and_dispatch()
#9 phar:///usr/local/bin/drush8/includes/startup.inc(465): drush_main()
#10 phar:///usr/local/bin/drush8/includes/startup.inc(369): drush_run_main(false, '/', 'Phar detected. ...')
#11 phar:///usr/local/bin/drush8/drush(114): drush_startup(Array)
#12 /usr/local/bin/drush8(10): require('phar:///usr/loc...')
#13 {main}

1 Answer 1


You need to manually delete the entities. Drupal will not allow you to have it as part of the uninstall, as that could delete user content without prompt, which would be a disaster. As far as I know, there is no way around this, as hook_uninstall() will be called after the pre-check to see if there is any content in the database.

For better UX, I usually create both a drush command that deletes my content entities, then an admin page that does the same thing. This way users can easily delete the content then uninstall the module.

  • Interesting. It quite happily will allow me to delete related vocabularies, views, as well as deleting custom tables created in the install, automatically. So if there are 1,000 pieces of content, I can write a module method to remove them prior to uninstalling, but can’t put it on the uninstall. Seems to me that building in and forcing a drush prompt in this case would make more sense.
    – JAyenGreen
    Jun 7, 2022 at 15:26
  • 1
    I believe you can delete configuration (vocabularies etc) but not content entities.
    – Jaypan
    Jun 7, 2022 at 15:33

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.