You can do it manually;
function clearLocalStorage(exclude) { for (var i = 0; i < localStorage.length; i++){ var key = localStorage.key(i); if (exclude.indexOf(key) === -1) { localStorage.removeItem(key); } } }
Please note that I deliberately used a lengthy iteration method over the length of localStorage and retrieved the match key, not just using for/in , because the for/in of the localStorage key localStorage not specified in the specification. Older versions of FireFox barf when you are for/in . I am not sure about the later versions ( more ).
exclude is expected an array of keys that you want to exclude from deletion;
clearLocalStorage(["foo", "bar"]);
Matt
source share