Invalidating and switching SDK instances
Introduction
Invalidating a Zendesk SDK instance means stopping and cleaning up the current SDK session. This process removes user data, closes real-time connections, and ensures that no further messages or notifications are received until the SDK is re-initialized. Integrators may need to invalidate the SDK to protect user privacy, switch between brands or environments, or reset the SDK state.
Switching between SDK instances allows your app to support multiple Zendesk brands, environments, or user contexts at runtime. This is useful for applications that need to dynamically change the Zendesk configuration, such as multi-brand apps or apps that allow users to switch accounts. Properly invalidating the current instance before initializing a new one ensures data isolation and prevents issues with user state, push notifications, or conversation continuity.
When should you invalidate the SDK?
Common use cases include:
- Switching brands or environments: If your app supports multiple Zendesk brands or environments, invalidate the current instance before initializing a new one.
- Clearing sensitive data: When handling sensitive information, invalidate the SDK to ensure no data persists locally.
Logging out a JWT-authenticated user
To properly log out a user authenticated with a JWT:
- Call the SDK's
Logout()
method to end the session and remove the JWT from the SDK. - After logging out, call
ZendeskMessaging.Instance.Invalidate()
to clear all local SDK data and connections.
Differences between invalidate() and logout
Invalidate()
clears local SDK state, user data, and closes connections, but does not log out a JWT user on the backend.Logout()
should be used to end the user's session and invalidate the JWT both locally and on the backend.
Important caveats when invalidating instances
- After invalidation, no messages will be received until the SDK is re-initialized.
- If you clear storage, all local data (including conversations and user state) is lost.
Code samples
C# – Invalidate
ZendeskMessaging.Instance.Invalidate();
C# – Switching between instances
// Initialize a new instance with a different brand or environment
await ZendeskSdk.InitializeAsync(options =>
{
options.ChannelId = "NEW_CHANNEL_ID";
}
);