Home > Blockchain >  How to clear the data from DataStore Preferences in Android [Kotlin]
How to clear the data from DataStore Preferences in Android [Kotlin]

Time:09-27

I want to know how can I clear the data from Data Store Preferences in Android. In my use-case when the user presses the Logout I want to clear the saved authentication code from the data store preferences.

Upon searching I found there was a way to do it in Shared Preferences using edit() and clear() method, but I can't find any such methods in Data Store Preferences. Also the blog by Android developers doesn't have any information about it.

CodePudding user response:

You can clear your preferences using this way

requireContext().dataStore.edit {
it.remove(key)
}

CodePudding user response:

As mentioned by @Shafaat and @DarShan there are two ways to clear data from DataStore Preferences.

  1. Clearing all the data in the data store preferences
context.dataStore.edit { preferences ->
    preferences.clear()
}
  1. If you want to remove the specific token (value) from DataStore
context.dataStore.edit { preferences ->
    preferences.remove(YOUR_TOKEN)
}
  • Related