Sub topic 14
Upgrades, deprecation and uninstalling
This chapter assumes you can already build, promote and install a package version. It covers what happens to an org when you install a newer version, how removed components are handled, and how to uninstall a package.
How an upgrade works
You upgrade an unlocked package by installing a newer released version over the installed one. Salesforce compares the two versions and applies the difference. It adds new components, updates changed ones, and handles components that are no longer in the package.
Two rules limit what you can do:
- You cannot install an older version over a newer one. There is no downgrade.
- A package that other installed packages depend on can only move to a version that still satisfies those dependencies.
What happens to removed components
Suppose version 1.1.0 of Core no longer contains an Apex class that 1.0.0 had. You removed the file from force-app/core and built a new version. During the upgrade, Salesforce has to decide what to do with that class in the target org.
The --upgrade-type flag of sf package install controls this:
| Upgrade type | Behaviour for removed components |
|---|---|
| `Mixed` | The default. Most removed components are deleted. Custom objects and fields are only deprecated, so data is not lost. |
| `DeprecateOnly` | Nothing is deleted. Removed components are marked as deprecated and left in the org. |
| `Delete` | Removed components are deleted, except custom objects and custom fields that cannot be deleted safely. |
sf package install --package "[email protected]" --target-org MyProdOrg --upgrade-type DeprecateOnly --wait 20A deprecated component no longer belongs to the package. It is still in the org, and an admin can delete it by hand once nothing uses it.
Plan removals in two steps
Deleting a field that reports, flows or Apex still reference will fail the upgrade. A safer pattern is:
- Release a version that stops using the component, but keeps it in the package.
- Install that version everywhere, then check that nothing references the component.
- Remove the component from source and release the next version.
Uninstalling a package
Uninstalling removes the package's components from the org.
sf package uninstall --package "[email protected]" --target-org MyProdOrg --wait 20Keep these points in mind:
- Uninstall fails if another installed package depends on this one. Uninstall Sales before Core.
- Data stored in the package's custom objects is deleted. Export what you need first.
- Test the uninstall in a sandbox before you run it in production.
Removing versions from the Dev Hub
Beta versions that you no longer need can be deleted with sf package version delete. Released versions cannot be deleted, because orgs may have them installed.
Exercise
You removed a custom field from Core's source and released version 1.1.0. You want production to keep the field and its data after the upgrade. What do you do?
Show answer
Install 1.1.0 with --upgrade-type DeprecateOnly. The field is then marked deprecated and stays in the org with its data. With Mixed, custom fields are deprecated and not deleted, but DeprecateOnly makes the intent explicit and applies it to every removed component.