34

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?

Merr Leader
  • 745
  • 2
  • 10
  • 13

1 Answers1

75

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.

Community
  • 1
  • 1
id.ot
  • 3,071
  • 1
  • 32
  • 47