I created entity type my_entity in my module. But now I don't need this entity anymore and I want to completely remove it without module's uninstall. I want to remove both definition and all database related tables or data. I thought about using \Drupal::entityDefinitionUpdateManager->uninstallEntityType('my_entity') in hook_update_N, but I can't use it because entity's definition will be removed.

Could anyone help me, and give advice how to delete both definition and database data?

UPD: I created an issue on drupal.org about this https://www.drupal.org/node/2655162

3 Answers 3


The argument for entityDefinitionUpdateManager->uninstallEntityType() is an EntityTypeInterface, not a string.

Drupal stores the last known state of an entity type, you can get it like this:

$entity_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $entity_update_manager->getEntityType('your_entity_type_id');

Note that this is using the enitty definitions update manager to get the entity type definition, not the runtime entity type manager.

The field definitions are also read from state and both the entity type and field definitions are purged afterwards.

  • Simplifed the code example.
    – Berdir
    Commented Jan 19, 2016 at 8:08
  • 1
    Unfortunately, this code works only if definition is present in code. If definition is removed from code I get the following error: The "my_entity" entity type does not exist. Commented Jan 19, 2016 at 10:41
  • Are you sure that you did get the entity type definition from the definition update manager and not the entity manager? It should definitely work. If not then that's a core bug.
    – Berdir
    Commented Jan 19, 2016 at 11:03
  • Yes, my code is the following: /** * Change definitions of entity tables. */ function mymodule_update_8101() { $entity_update_manager = \Drupal::entityDefinitionUpdateManager(); $entity_type = $entity_update_manager->getEntityType('my_entity'); $entity_update_manager->uninstallEntityType($entity_type); } And it works only if entity definition is present in code. Commented Jan 19, 2016 at 11:56
  • Open a core issue then. I think there is no test for this yet, as it is tricky to reproduce in a test. We need to add one. Try to figure out more about that error, a stack trace of the exception would be good to have. Maybe the problem is in some module that reacts to the event.
    – Berdir
    Commented Jan 19, 2016 at 12:09

You can also try

function MODULE_NAME_post_update_8104(&$sandbox) {
  // Making sure the entities are deleted.
  try {
    $storage_handler = \Drupal::entityTypeManager()
    $entities = $storage_handler->loadMultiple();
  catch (PluginException $e) {
  catch (EntityStorageException $e) {
  // Uninstalling the entity afterwards.
  $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $entity_update_manager->getEntityType('ENTITY_TYPE');
  1. Delete all the entities of that type.
  2. Remove the entity type declaration
  3. Rebuild the caches
  • Unfortunatly, everything isn't so easy. Despite of entity declaration there is also data in database(for example base data) which should be deleted. Commented Jan 18, 2016 at 20:11
  • @MariaKvitova that needs to be done manually. I'll update the answer with a more detailed explanation.
    – Eyal
    Commented Jan 18, 2016 at 20:25
  • That's not correct, see my answer. This is absolutely possible.
    – Berdir
    Commented Jan 18, 2016 at 20:43

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.