The RudderStack Android (Kotlin) and iOS (Swift) SDKs provide a reset API that lets you clear the persisted data, for example, user ID and traits.
The SDK also does the following once you call the reset API:
Generates a new anonymousId (default behavior)
If session tracking is enabled, clears the current sessionId and generates a new one.
If a mobile device mode integration plugin is present, then calling the reset API also triggers the integration’s reset API — provided it is supported.
Selective reset: Choose specific data points to reset while preserving others
Default behavior
By default, calling the reset API clears all persisted user data including user ID and traits, refreshes session information, and generates a new anonymous ID.
analytics.reset()
The corresponding Java snippet is shown below:
analytics.reset();
analytics.reset()
The corresponding Objective-C snippet is shown below:
[analyticsreset];
Selective reset
You can also use the reset API to choose which specific data components to reset while preserving others. This way, you have granular control over each component — you can regenerate the anonymous ID, clear the user ID, clear traits, or refresh session information, or preserve any of these values as per your requirement.
To perform a selective reset, the SDKs provide a options parameter that accepts a ResetOptions object with the following structure:
The following table describes the parameters accepted by the entries object of ResetOptions:
Parameter
Description
Default value
anonymousId
Resets the anonymous user ID.
true
userId
Resets the persisted user ID.
true
traits
Resets the persisted user traits.
true
session
Resets the current session information.
true
All entries in the ResetOptions.entries object are optional. If you do not specify any entry, the SDK sets its default value as described in the above table.
Examples
This section provides the following examples of performing a selective reset using the reset API:
The identify API internally calls reset() when the userId changes. This is an implicit reset — it uses default ResetOptions (all entries reset to true). See the following scenarios for more information:
Scenario
Reset triggered?
What is reset
identify on anonymous user (no prior userId)
No
Nothing
identify with the same userId
No
Nothing
identify with a different userId (User A > User B)
Yes
anonymousId regenerated
userId cleared
traits cleared
sessionId refreshed
You cannot pass custom ResetOptions to the implicit reset. To preserve specific entries (for example, to keep the session intact during a user switch), call reset explicitly with your desired options before calling identify.
Why this works: The explicit reset call clears the userId (since userId = true). When you call identify subsequently, the SDK sees that no previous userId is set - so it treats this as an anonymous-to-identified transition and skips the implicit reset entirely. This way, any entries you chose to preserve in the explicit reset (for example, session = false) remain untouched.
// Preserve session while switching users
analytics.reset(ResetOptions(entries=ResetEntries(anonymousId=true,userId=true,// Should be set to `true` — otherwise the approach will not work.
traits=true,session=false// Preserve session
)))analytics.identify(userId="new-user-id")
The corresponding Java snippet is shown below:
// Preserve session while switching users
ResetEntriesBuilderentriesBuilder=newResetEntriesBuilder().setAnonymousId(true).setUserId(true)// Should be set to true — otherwise this approach won't work.
.setTraits(true).setSession(false);// Preserve session
ResetOptionsBuilderoptionsBuilder=newResetOptionsBuilder().setEntries(entriesBuilder.build());analytics.reset(optionsBuilder.build());analytics.identify("new-user-id");
// Preserve session while switching usersanalytics.reset(options:ResetOptions(entries:ResetEntries(anonymousId:true,userId:true,// Should be set to `true` — otherwise the approach will not work.traits:true,session:false// Preserve session)))analytics.identify(userId:"new-user-id")
The corresponding Objective-C snippet is shown below:
// Preserve session while switching users
RSSResetEntriesBuilder*entriesBuilder=[RSSResetEntriesBuildernew];[entriesBuildersetAnonymousIdResetEntry:YES];[entriesBuildersetUserIdResetEntry:YES];// Should set to YES — otherwise this approach won't work.
[entriesBuildersetTraitsResetEntry:YES];[entriesBuildersetSessionResetEntry:NO];// Preserve session
RSSResetOptionsBuilder*optionsBuilder=[RSSResetOptionsBuildernew];[optionsBuildersetEntries:[entriesBuilderbuild]];[analyticsresetWithOptions:[optionsBuilderbuild]];[analyticsidentify:@"new-user-id"];
Questions? We're here to help.
Join the RudderStack Slack community or email us for support
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.