Update or Delete an OAuth Named Credential

Here are some examples of how you can update or delete a credential from Apex. These code examples reference and build on the Apex code in Create an OAuth Named Credential which creates a named credential that connects to GitHub.

For security reasons, if your managed package code (Apex or Metadata API) programmatically updates a named credential, Salesforce automatically disables callouts for that credential. This safety mechanism prevents malicious code from silently redirecting an authenticated connection to an attacker-controlled server.

After your package updates a credential, the connection won’t immediately work. You must instruct the subscriber's admin to turn on the Enabled for Callouts field in the named credential. See Create or Edit a Named Credential in Salesforce Help.

In this example, the auth provider authorization URL is updated.

In this example, the external credential’s scope for GitHub access is updated to full.

In this example, the named credential’s callout URL is updated.