Search code examples
androidkotlinsharedpreferencesdatastoreandroid-jetpack-datastore

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


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.


Solution

  • You can clear your preferences using this way

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