Home > Mobile >  AsyncStorage.removeItem is not removing the item
AsyncStorage.removeItem is not removing the item

Time:11-16

There are so many questions available and I have tried almost about 12 different ways but none are working. The most efficient one is:

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

Then I'm calling it as:

  useEffect(() => {
    removeItemValue('@cartInfo');  
  }, []);

I've tried putting it outside of useEffect hook but still no effect. What am I doing wrong?


UPDATE:

Tried this as well but didn't work:

  useEffect(() => {
    removeItemValue('@cartInfo').then(() => { console.log('removed') })
  }, []);

Also tried

  useEffect(() => {
    AsyncStorage.removeItem('@cartInfo', () => {
      AsyncStorage.getItem('@cartInfo').then((res) => {
        console.log("RES IS: "   res);
      })
    })
  }, []);

Still no luck. I'm using v1.12.1 of @react-native-community/async-storage

CodePudding user response:

As we discussed in chat, AsyncStorage.removeItem was actually working, and the issue is that setItem was being called too often, so that the value was replaced before being read later in getItem.

  • Related