How to uninstall a module that has added a basefield that has data

by Jonathan   Last Updated November 12, 2017 11:07 AM

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'];

Answers 2

Does drush field-delete myfieldname work for base fields? If so, you could look in drush's source code to see how it accomplishes this.

I don't see any information regarding this in the drush 8.x documentation.

September 26, 2016 02:58 AM

This is a known Drupal Core limitation that looks close to being ready so hopefully will be fixed in D8.5. In the meantime you could apply the patch linked to the issue.

November 12, 2017 10:25 AM

Related Questions

Can't uninstall module because of checkbox field

Updated March 10, 2017 11:07 AM