Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Delete field while uninstalling module
Hi Prashanth,
You can use hoot_uninstall. Refer code below:
function modulename_uninstall(){
// Remove the fields
\Drupal::configFactory()->getEditable('your_field_confoguration ')->delete();
}
Doesn't work?
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".
"Enforced" module in yml file
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.
How would you export your
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 😞