Sub topic 14
Upgrades, deprecation and uninstalling
This chapter covers what happens to an org after the first install. You will upgrade an installed package, see what happens to components you removed, and remove a package safely. It assumes you can already build, promote and install a package version, and that you know how dependencies between packages are declared.
How an upgrade works
An upgrade is an install of a higher version of a package that is already in the org. You use the same command, sf package install. Salesforce compares the components in the installed version with the components in the new version, then adds, changes or removes components to match.
Three rules apply:
- The new version number must be higher than the installed one. You cannot install an older version over a newer one, so a rollback means releasing a new, higher version with the old content.
- Production only accepts released versions. Promote the version first.
- Packages that depend on the one you upgrade must still work with the new version. The dependency rules from the previous chapters still apply.
You can see what is installed before you change anything:
sf package installed list --target-org MyProdOrgWhat happens to removed components
Suppose version 1.0 of Core contains the custom field Account.Legacy_Code__c, and version 1.1 does not. During the upgrade Salesforce must decide what to do with that field. You control the decision with --upgrade-type.
| Upgrade type | Behavior for components missing from the new version |
|---|---|
| `Mixed` (default) | Deletes components that can be safely deleted and deprecates the rest. |
| `DeprecateOnly` | Deprecates every removed component and deletes nothing. |
| `Delete` | Deletes removed components, except custom objects and custom fields that have dependencies. |
Deprecating a component removes it from the package but leaves it in the org. Nothing is lost, and it is no longer managed by the package. You can delete it later by hand, or move it into another package. Deleting a component is permanent, and for a custom field that includes its data.
sf package install --package "[email protected]" --target-org MyProdOrg --upgrade-type DeprecateOnly --wait 20 --no-prompt--upgrade-type applies only to unlocked packages. On a first install the flag has no effect, because nothing has been removed yet.
Removing components safely
Removal is a two-release process in a busy org. This order avoids most failures.
- In release N, stop using the component. Remove references from Apex, flows, page layouts, permission sets and reports.
- In release N+1, remove the component from the package directory and build a new version.
- Install the new version in a sandbox that copies production and check the result.
- Upgrade production with
DeprecateOnly. Delete the leftover components yourself once you know they are unused.
References are the usual cause of trouble. A component that something else uses cannot be deleted, so Salesforce deprecates it instead. A component that another package uses is a dependency, and removing it can block the upgrade.
Dependencies during upgrades
Installing an upgrade of Sales can require a newer Core. In that case, upgrade Core first, then Sales. The version you declare as a dependency, such as 1.0.0.LATEST, is resolved when you create the version. The installed packages in each org may be older than that. Always install dependencies before the packages that use them.
The opposite order matters for removal. You must uninstall dependents before the package they depend on.
Uninstalling a package
Uninstalling removes the package and all of its components from the org:
sf package uninstall --package "[email protected]" --target-org MyProdOrg --wait 20If the command times out, check the request with the ID it printed:
sf package uninstall report --request-id 06y000000000001AAA --target-org MyProdOrgKnow these points before you run it:
- The components are deleted. For custom objects and fields that includes the records stored in them.
- Uninstall fails if another installed package depends on this one. Uninstall Sales before Core.
- Deleted components go to the Recycle Bin for a limited time, so a mistake can sometimes be recovered. Do not rely on this for important data. Export it first.
- Components that were deprecated by earlier upgrades are no longer part of the package, so uninstalling does not remove them.
Cleaning up versions in the Dev Hub
Uninstalling affects an org. Deleting affects the Dev Hub record. You can delete a beta version that you no longer need:
sf package version delete --package "[email protected]" --target-dev-hub MyDevHub --no-promptReleased versions are different. Orgs may have installed them, and a released version cannot go back to beta. Plan to publish a fixed higher version rather than trying to withdraw a released one.
Common errors
- The upgrade fails because a removed component is still referenced. Find the reference, remove it, and rebuild. Or use
DeprecateOnlyand clean up later. - The install says the version is lower than the installed one. You cannot downgrade. Build a new version with a higher number.
- Uninstall fails because of a dependency. List installed packages and uninstall the dependent first.
- A deprecated field keeps appearing in the org. This is expected. It is no longer packaged, so delete it manually once it is unused.
- A production install fails because the version is beta. Promote the version, then install it.
Exercise
Version 1.2 of Core removes a custom object that holds customer records. You must upgrade production without losing data. Which --upgrade-type should you use, and what do you do afterwards?
Show answer
Use DeprecateOnly. The object stays in the org with its data but is no longer part of the package. Afterwards, export the data if you need it, confirm that nothing references the object, and then delete it manually, or move it into another package.
Exercise
Sales depends on Core, and both are installed in a sandbox. You want to remove both. In which order do you uninstall them, and why?
Show answer
Uninstall Sales first, then Core. Uninstalling Core while Sales is installed fails because Sales depends on it. Remove the dependent package before the package it depends on.
Exercise
A teammate wants to roll production back from Core 1.3.0-1 to 1.2.0-1 by installing the older version. Why will this not work, and what should they do?
Show answer
An install cannot lower the version of an installed package. They should create a new version, such as 1.3.1, whose content matches 1.2.0, promote it, and install that as an upgrade.