Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I force refresh of the entire local storage?

I am creating a shopping cart.
Basically, when I reload the page with the products in my cart, the local storage doesn’t refresh, which is what I want.

But I also want it to refresh at some point only (when I open another page). How do I get the local storage to refresh completly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

localStorage.clear()

The clear() method of the Storage interface clears all keys stored in a given Storage object.

for example

//feeds storage
localStorage.setItem('foo', 'bar');
localStorage.setItem('john', 'wick');
localStorage.setItem('joe', 'doe');

//lets get the value bar from the localStorage
console.log(localStorage.getItem('foo'))//outputs 'bar'

//clears all data
localStorage.clear();

//now if you try to do it again after you clear it will print null
console.log(localStorage.getItem('foo'))//outputs null
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading