My module adds a basefield 'myfield' to the core 'file' content entity.
I now need to uninstall my module for development purposes, but I can't because drush refuses to uninstall the module, saying "There is data for the field myfield on entity type File".
My understanding is that this is a known issue:
Because my module adds this field to an entity it does not own, this uninstallation will not be taken care of automatically by the core uninnstall procedure,
Therefore my module needs to have a hook_uninstall that triggers field uninstallation using \Drupal::entityDefinitionUpdateManager()->uninstallFieldStorageDefinition
However, a field can't be uninstalled if it still has data in it, so I first need to remove the data.
But the system to purge field data currently only works for bundle fields not base fields.
There may be other ways to delete field data, but they won't scale to large quantities of data.
Therefore there is no robust way to uninstall my module and Drupal core is correct to refuse to uninstall it.
Given all that, how best can I uninstall it when I need to do so for development purposes (short of reinstalling Drupal)?
Here is how I am creating it:
In mymodule.module:
function mymodule_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'file') {
$fields = array();
$fields['myfield'] = BaseFieldDefinition::create('boolean')
->setLabel(t('My field'))
return $fields;
}
}
In mymodule.install:
$entity_manager = \Drupal::entityManager();
$definition = $entity_manager->getFieldStorageDefinitions('file')['myfield'];
$entity_manager->onFieldStorageDefinitionCreate($definition);