Search code examples
react-nativeasyncstorage

Is it possible to remove all items from AsyncStorage


I'm testing my React-Native application and want to remove all items from AsyncStorage in order to test the app from the beginning. And I am a bit confused.

I read the official documentation and found multiRemove and clear functions, but I cannot understand how to clear all items of my application (clear as far as I understood clear the whole storage of all applications and I'm afraid to use it), and multiRemove delete only keys that I give it in parameters, but I want to clear all keys.

I suppose I can do it through getAllKeys keys-values and remove it one-by-one, but maybe there is a more clear way to do it? :)

thanks

P.S: I tried to to like this:

clearAllData() {
    AsyncStorage.multiRemove([]).then(() => alert('success'));
}

but it doesn't work...


Solution

  • I suppose I can do it through getAllKeys keys-values and remove it one-by-one, but maybe there is a more clear way to do it? :)

    You should do that, that's the only way to remove all keys from your app.

    Here is a simple way of doing it:

    clearAllData() {
        AsyncStorage.getAllKeys()
            .then(keys => AsyncStorage.multiRemove(keys))
            .then(() => alert('success'));
    }