Hello All,

I have added a field in the user section using yml file which is added in "config/install" location in my custom module.
I want to delete this field when ever the module is uninstalled, could not find appropriate code to do this.

Can someone please point me in the right direction.

--
Best
Prashanth

Comments

Adarshsri786’s picture

Hi Prashanth,

You can use hoot_uninstall. Refer code below:

function modulename_uninstall(){
// Remove the fields
\Drupal::configFactory()->getEditable('your_field_confoguration ')->delete();
}

kopeboy’s picture

I've implemented that and although it deletes the configuration, the database tables are kept, even after running cron 🤨
If I go to admin/reports/status#error I can infact see a list of "The my_entity.my_field field needs to be uninstalled".

bernard.cal’s picture

I just had the same problem, and after a lot of research and trials and errors, I found this:
In the yml config files for both the field itself (field.field.your_field...) and its storage (field.storage.your_field...), add in the module dependencies the "enforced" line:
enforced:
module:
- your_module_name
I think this will also preclude other modules to use your custom field.

kopeboy’s picture

How would you export your configuration as enforced instead of manually editing each of the many .yml files?!

EDIT: how could I not think of it.. there's a module for that! Just found config_enforce_devel 😅
EDIT EDIT: despite the name, that module is not currently putting config depencies as "enforced" in the yamls 😞