Skip to content Skip to sidebar Skip to footer

How To Clear Jetpack Datastore Data On Specific Condition

i've been using jetpack datastore for a while, but then i got a problem. I want to clear data in datastore when the app is destroyed. Im using jetpack datastore to persist data onl

Solution 1:

Use this

dataStore.edit { 
        it.clear()
    }

Method description states

Removes all preferences from this MutablePreferences.

Solution 2:

In case anyone wants to know how to remove a specific preference

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

Solution 3:

Try this (for Proto DataStore):

 dataStore.updateData { obj ->
    obj.toBuilder()
        .clear()
        .build()
 }

Solution 4:

For Proto DataStore you can do:

dataStore.updateData { it.getDefaultInstance() }

It doesn't delete the file, but it's effectively the same.

Post a Comment for "How To Clear Jetpack Datastore Data On Specific Condition"