I set an item in localstorage in this way:
localStorage.setItem("test", "testing");
Now, I try to remove it but it's not working. I found and tried the following solutions without any result:
localStorage.removeItem("test");
localStorage.removeItem(0);
window.localStorage.removeItem(0);
window.localStorage.removeItem("test");
Is there another way to do it?
I set an item in localstorage in this way:
localStorage.setItem("test", "testing");
Now, I try to remove it but it's not working. I found and tried the following solutions without any result:
localStorage.removeItem("test");
localStorage.removeItem(0);
window.localStorage.removeItem(0);
window.localStorage.removeItem("test");
Is there another way to do it?
Share Improve this question asked Apr 19, 2017 at 1:40 MaxMax 1052 silver badges11 bronze badges 7- How are you verifying that it doesn't work? – Phil Commented Apr 19, 2017 at 1:43
- The APIs you're using are correct. Like @Phil said how are you verifying this? Are you opening the developer console and checking there? – Darkrum Commented Apr 19, 2017 at 1:46
-
@Phil, @Darkrum I'm checking with this :
var test_value = localStorage.getItem("test");
Then :console.log(test_value);
– Max Commented Apr 19, 2017 at 1:50 -
@MArbez do you do that before or after removing the item? The value returned by
localStorage.getItem
is a string and thus, immutable. It is not an object reference if that's what you were expecting – Phil Commented Apr 19, 2017 at 1:52 - Ok, your example is working. Now, I'm trying on an array stored as JSON (this was the final goal of my question). – Max Commented Apr 19, 2017 at 2:05
2 Answers
Reset to default 6I found the problem. It wasn't local storage, it was due to the order of executions of my functions. In my first function, I set the local storage variable. In the second one, I use it and remove it. The problem was that the second function was executed faster than the first one. So the data was deleted and then, set again. That gave me the impression that local storage deletion wasn't working.
Your problem may be caused by your local hosting
this problem happened to me and I always thought that the problem was from my code, but later it became clear to me that the problem is from the local hosting
I used this address in my project:
http://192.168.1.10:8080
And then I replaced the address with this:
http://127.0.0.1:8080
The problem is solved and now I can delete all LocalStorage from the inspect.