When uninstalling a managed package the process fails with the message:
Unable to uninstall package
Component Type: Record Type
Problem: This installed component is referenced by a locally created component. (link to a particular record)
This makes sense, I need to remove the Record Type from local records that are currently using it before uninstalling it.
Can I do this with an apex uninstall script or does it need to be done manually by an admin before attempting the uninstall?
To remove references to the record type manually I mark it as inactive in the admin UI. This may require the additional steps, such as removing the record type as the default from any profiles that are using it.
I can set the RecordTypeId via apex, but I'm not sure what to change it to. I can't see the master record type as a valid value to assign and there may be no other record types for the sObjectType.
Another more drastic approach is to delete all the records that reference that RecordType (and then empty the recycle bin). This is fine for things like Product2 where in my case the records are only applicable to the managed package, but not so great when dealing with Accounts and Contacts.
Database.emptyRecycleBin(deletedProducts);
immediately after deleting a list of products helps with the first issue. But you are right, as the number of records increases an uninstall script will run into limit issues.