how to remove an item from AsyncStorage in react-native

This is what I did, had a similar issue.It works well when you want to remove an item based on its id. make sure each item has a unique id.

remove_user = async(userid) => {
    try{
        let usersJSON= await AsyncStorage.getItem('users');
        let usersArray = JSON.parse(usersJSON);
        alteredUsers = usersArray.filter(function(e){
            return e.id !== userid.id

        })
        AsyncStorage.setItem('users', JSON.stringify(alteredUsers));
        this.setState({
           users:alteredUsers
        })
    }
    catch(error){
        console.log(error)
    }
};

This looks correct, but maybe you are trying to read back from AsyncStorage too soon? It's asynchronous, so the change isn't applied right away and you might still see the key if you try to get it on the following line. Try to call AsyncStorage.removeItem with await or do what you want to do in the callback.


Try this:

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

Tags:

React Native