I have a lot of items in localstorage, and I want to clear them all...except 1 item, we'll call it "X".
Is there a way to call localstorage.clear() but exclude X?
removeItem() : How to delete localStorage sessions To delete local storage sessions, use the removeItem() method. When passed a key name, the removeItem() method removes that key from the storage if it exists. If there is no item associated with the given key, this method will do nothing.
The clear() method removes all the Storage Object item for this domain. The clear() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.
Local Storage data will not get cleared even if you close the browser. Because it's stored on your browser cache in your machine. Local Storage data will only be cleared when you clear the browser cache using Control + Shift + Delete or Command + Shift + Delete (Mac)
Store the value you'd like to keep in another variable, then use localStorage.clear()
Example:
var myItem = localStorage.getItem('key'); localStorage.clear(); localStorage.setItem('key',myItem);
Example was taken from this SO post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With