I'm trying to delete some config entities in an install hook. These entities do not exist in the config sync folder, they're excluded using the config_exclude_modules approach in settings.php. So deleting them from the config sync folder isn't an option, it needs to be done by running some code.

Contrived example:

function module_install() {
  $entities = [
    'foo' => ['bar', 'baz'],
    // ...

  foreach ($entities as $type => $ids) {
    $storage = \Drupal::entityTypeManager()->getStorage($type);

This works fine on its own; if I drush en module, the entities are deleted as expected.

This module does not exist upstream, so as part of the commit core.extension.yml is updated to enable the module. When everything's pushed, running drush cim upstream installs the module as expected, and the install hook is run.

But, as part of the same cim, all of the config entities I just deleted are recreated. I guess this kind of makes sense; the target state was calculated before the module was installed, and the import process is dutifully making sure the result will match that initial calculation when it finishes? Maybe?

This is annoying, though, as being able to delete these entities in an install hook would be extremely useful for a particular project. Alternatives like running a command against all of the remote sites will be difficult to organise, and using the natural CM flow already in place would save a lot of pain.

Is there a way to make this work?

Your Answer

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

Browse other questions tagged or ask your own question.