Search code examples
javascript

Clear local storage but exempt certain values.


Is there a way to clear window.localStorage i.e window.localStorage.clear(); but exempt certain key/value pairs?


Solution

  • No, but you can save the values of what you want in a variable and then clear the localStorage and then add the items stored in the variable to it again.

    Example:

    var myItem = localStorage.getItem('key');
    localStorage.clear();
    localStorage.setItem('key',myItem);