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.
As mentioned by @Shafaat and @DarShan there are two ways to clear data from DataStore Preferences.
context.dataStore.edit { preferences ->
preferences.clear()
}
context.dataStore.edit { preferences ->
preferences.remove(YOUR_TOKEN)
}
You can clear your preferences using this way
requireContext().dataStore.edit {
it.remove(key)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With