Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using localstorage.clear() but exclude 1 item

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?

like image 822
Merr Leader Avatar asked Dec 02 '14 20:12

Merr Leader


People also ask

How do I remove one item from localStorage?

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.

What does localStorage Clear () do?

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.

Does clearing localStorage clear cache?

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)


1 Answers

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.

like image 101
id.ot Avatar answered Sep 22 '22 23:09

id.ot