Search code examples
react-nativeasyncstorage

How to remove a single item from asyncStorage in react native?


I have a list of Items stored with asyncStorage, they all have the same key, using await AsyncStorage.removeItem(key) function its removes the entire list of Items is there a way to remove only one single Item.

async removeItemValue(key) {
  
  try {
    await AsyncStorage.removeItem(key);
    return true;
  }
  catch(exception) {
    return false;
  }

Solution

  • What I will do:

    1. Get the list
    2. remove the item
    3. save the new list.

    Something like:

    const value = await AsyncStorage.getItem(theKey);
    if (value !== null){
         var index = array.indexOf(theItem);
         if (index > -1){
             value.splice(index, 1);
         }
    }
    AsyncStorage.setItem(theKey, value);