213

I need to clear all data i set into localStorage. By this, I mean completely reset localStorage to null when users remove their accounts.

How can i do that with a simple function?

I tried this:

function clearLocalStorage(){
    return localStorage= null;
}

But it doesn't work as expected.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
itsme
  • 48,972
  • 96
  • 224
  • 345
  • Just in case it helps someone, the code above is wrong in many ways. First of all, the `=` operator is meant to be used in a sentence, and the result shouldn't be used as an expression (look for differences between sentence and expression, two concepts that are related). Also, assigning null to a variable clears the value of it, but not necessarily clears the object that it contains from the memory (e.g. if the object is referenced by other variables). – Ferran Maylinch Dec 11 '22 at 13:51

5 Answers5

534

localStorage.clear();

should work.

frenchie
  • 51,731
  • 109
  • 304
  • 510
Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
114

If you want to remove/clean all the values from local storage than use

localStorage.clear();

And if you want to remove the specific item from local storage than use the following code

localStorage.removeItem(key);
Talha
  • 18,898
  • 8
  • 49
  • 66
  • Hi, @Talha. If I don't want to remove for a particular item then is there a way to do it too? For example 'removeItemNotIn(key)'. Thanks in advance. – Vibhav Chaddha Apr 12 '18 at 06:02
  • I figured out a solution for my problem:- if(localStorage.getItem(particularKey) == null){ localStorage.clear(); } But if you have a better solution then do let me know. Thanks. – Vibhav Chaddha Apr 12 '18 at 07:45
13

It only worked for me in Firefox when accessing it from the window object.

Example...

window.onload = function()
{
 window.localStorage.clear();
}
John
  • 1
  • 13
  • 98
  • 177
10

Using .one ensures this is done only once and not repeatedly.

$(window).one("focus", function() {
    localStorage.clear();
});

It is okay to put several document.ready event listeners (if you need other events to execute multiple times) as long as you do not overdo it, for the sake of readability.

.one is especially useful when you want local storage to be cleared only once the first time a web page is opened or when a mobile application is installed the first time.

   // Fired once when document is ready
   $(document).one('ready', function () {
       localStorage.clear();
   });
4

Something like this should do:

function cleanLocalStorage() {
    for(key in localStorage) {
        delete localStorage[key];
    }
}

Be careful about using this, though, as the user may have other data stored in localStorage and would probably be pretty ticked if you deleted that. I'd recommend either a) not storing the user's data in localStorage or b) storing the user's account stuff in a single variable, and then clearing that instead of deleting all the keys in localStorage.


Edit: As Lyn pointed out, you'll be good with localStorage.clear(). My previous points still stand, however.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123