27

How to remove an item from AsyncStorage? right now I am trying this code:

AsyncStorage.removeItem('userId');

but this is not working for me.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
achu
  • 639
  • 2
  • 10
  • 26
  • Try adding the callback to removeItem and console log some output to make sure everything is working properly. `AsyncStorage.removeItem('token', (err) => console.log('userId', err));`Also provide more of your code – Farsheel Nov 30 '17 at 11:20
  • 1
    https://stackoverflow.com/questions/42627947/in-react-native-how-to-store-values-in-session/42628807#42628807 – jose920405 Nov 30 '17 at 13:43

8 Answers8

87

Try this:

async removeItemValue(key) {
    try {
        await AsyncStorage.removeItem(key);
        return true;
    }
    catch(exception) {
        return false;
    }
}
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
6

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)
    }
};
404notBrighton
  • 153
  • 1
  • 3
  • 10
2

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.

dhorelik
  • 1,477
  • 11
  • 14
1

this delete method which removes object from array by passing index (here i called id)

async deleteData(id)  {
      try {
        this.state.item.splice(id, 1);
        await AsyncStorage.setItem("mylist",JSON.stringify(this.state.item))
        this.setState({ item: JSON.parse(await AsyncStorage.getItem("mylist")) })

      } catch (error) {
        console.log(error);
      }
    };

and call this by using onPress method, here i am using button and pass index

<Button onPress={this.deleteData.bind(this,index)}>delete</Button>
1

Use removeItem() method to remove values from AsyncStorage in react.

try {
    await AsyncStorage.removeItem(key);
    console.log('Data removed')
}
catch(exception) {
    console.log(exception)
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

This is the framework code for AsyncStorage.removeItem:

removeItem: function(
 key: string,
 callback?: ?(error: ?Error) => void
): Promise {
 return new Promise((resolve, reject) => {
  RCTAsyncStorage.multiRemove([key], function(errors) {
    var errs = convertErrors(errors);
    callback && callback(errs && errs[0]);
    if (errs) {
      reject(errs[0]);
    } else {
      resolve(null);
    }
  });
 });
}

As you can see above it requires the key(Which is name of the item you set in asyncstorage that you want to delete) and a callback function. Make sure you have the correct key and the correct params and it should work fine.

ShaneG
  • 1,498
  • 7
  • 16
  • 1
    So I see this in the documentation, but I don't understand how to actually *use* it. The syntax is indecipherable to me. Can you show an example of how one would write the code to delete a item from the storage, given the key of the item that is to be deleted? Many Thanks in Advance. – SherylHohman Apr 24 '18 at 12:39
  • Also I see many are using await.. is it possible to just use promises and .then and .catch ? – SherylHohman Apr 24 '18 at 12:41
0

try this for react-native

const deletCar = async () => {
        try {
            await AsyncStorage.removeItem('@storage_Key').then(() => {
               
                // props.navigation.navigate('same page name refresh ');   
               /* setprevCar({ carNumner: '', carModel: '' }) return  empty obj if deleted. */
            })


        }
        catch (exception) {
            return false;
        }

    }
Omar bakhsh
  • 896
  • 11
  • 18
0

if you want to remove the selected list :-

 let key =[
    'visitorId',
    'visitorMobile',
    'visitorData',
  ]

await AsyncStorage.multiRemove(key)
Deepak Singh
  • 749
  • 4
  • 16