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:
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.