As the developer of an extension that creates its own custom fields, what can I do to cause those custom fields to be removed when the extension is uninstalled?


Your extension needs to include an Upgrader class containing an uninstall function. In there you can use the api to delete the custom fields and custom group. Generate the Upgrader class in civix using the command:

civix generate:upgrader

Read more documentation on this at https://docs.civicrm.org/dev/en/master/extensions/civix/#add-a-database-upgrader-installer-and-uninstaller


@coleman's answer is accurate, but there are less manual ways to approach this. See Tim's post API and the Art of Installation, which outlines the use of hook_civicrm_managed as well as a civix helper/naming convention *.mgd.php. Here's an example in CiviVolunteer, though it's for a different entity.

If you're adding custom tables, you may wish to avail yourself of another civix helper/naming convention. Your extension's Upgrader class automatically scans for files named *_uninstall.sql in the extension's sql directory when the extension is uninstalled. Here's another CiviVolunteer example.

  • That said, I don't think I've tried this the managed approach with entities that depend on one another. Maybe naming them so that they run in the right order (e.g., 001_customGroups.mgd.php, 002_customFields.mgd.php) will work, but I haven't tried it. Jan 31 '17 at 20:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.