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?
2 Answers
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. Commented Jan 31, 2017 at 20:35