4

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

6

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');
$entity_update_manager->uninstallEntityType($entity_type);

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 Jan 19 '16 at 8:08
  • 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. – Maria Kvitova Jan 19 '16 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 Jan 19 '16 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. – Maria Kvitova Jan 19 '16 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 Jan 19 '16 at 12:09
0
  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. – Maria Kvitova Jan 18 '16 at 20:11
  • @MariaKvitova that needs to be done manually. I'll update the answer with a more detailed explanation. – Eyal Jan 18 '16 at 20:25
  • That's not correct, see my answer. This is absolutely possible. – Berdir Jan 18 '16 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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