I have created a custom module and a configuration say 'abc_settings'. This configuration creates a variable for each vocabulary present on the site like vocab1_options, vocab2_options, vocab3_options etc. I want to delete 'vocab1_options' when vocabulary 1 (vocab1) will be deleted. How to achieve this ?

1 Answer 1


Deleting a config key:

Updating per Berdirs comment, deleting/clearing a key within a config object, not deleting the entire config object:

Drupal::configFactory()->getEditable('abc_settings')->clear($entity->id() . '_options')->save();

Deleting config objects:

I did this within TVI module, anytime a term or vocab is deleted:

 * Implements hook_ENTITY_TYPE_delete().
 * Remove TVI settings when vocabularies are deleted.
 * @param EntityInterface $entity
function tvi_taxonomy_vocabulary_delete(EntityInterface $entity) {
  Drupal::configFactory()->getEditable('tvi.taxonomy_vocabulary.' . $entity->id())->delete();

 * Implements hook_taxonomy_term_delete().
 * Remove TVI settings when terms are deleted.
 * @param EntityInterface $entity
function tvi_taxonomy_term_delete(EntityInterface $entity) {
  Drupal::configFactory()->getEditable('tvi.taxonomy_term.' . $entity->id())->delete();

Of course, you would pass your own config name to getEditable(), and the hook names need to have your module name and not tvi.

This will delete any config matching that name when a term or vocab is deleted.

I haven't taken the step yet in my own code, but you could go further and implement a method that before a vocabularly entity is deleted, get all of its children, loop and delete config, then delete the vocab and its config.


Drupal::configFactory()->getEditable($entity->id() . '_options')->delete();
  • 1
    Is that really what was asked? This deletes whole configuration objects, I understood the question as deleting keys in a configuration object. For that, you can use ->clear('some_key')->save().
    – Berdir
    Sep 9, 2016 at 20:12
  • From the names I inferred them as a config object per vocab. I see now..
    – Kevin
    Sep 9, 2016 at 20:21

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.