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?

0

1 Answer

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like