local storage – Clearing localStorage in javascript?

The Question :

790 people think this question is useful

Is there any way to reset/clear browser’s localStorage in javascript?

The Question Comments :

The Answer 1

1440 people think this answer is useful

Use this to clear localStorage:


The Answer 2

213 people think this answer is useful

If you want to remove a specific Item or variable from the user’s local storage, you can use

localStorage.removeItem("name of localStorage variable you want to remove");

The Answer 3

114 people think this answer is useful
window.localStorage.clear(); //try this to clear all local storage

The Answer 4

12 people think this answer is useful

Here is a function that will allow you to remove all localStorage items with exceptions. You will need jQuery for this function. You can download the gist.

You can call it like this

let clearStorageExcept = function(exceptions) {
  let keys = [];
  exceptions = [].concat(exceptions); // prevent undefined

  // get storage keys
  $.each(localStorage, (key) => {

  // loop through keys
  for (let i = 0; i < keys.length; i++) {
    let key = keys[i];
    let deleteItem = true;

    // check if key excluded
    for (let j = 0; j < exceptions.length; j++) {
      let exception = exceptions[j];
      if (key == exception) {
        deleteItem = false;

    // delete key
    if (deleteItem) {

The Answer 5

8 people think this answer is useful

Localstorage is attached on the global window. When we log localstorage in the chrome devtools we see that it has the following APIs:

enter image description here

We can use the following API’s for deleting items:

  1. localStorage.clear(): Clears the whole localstorage
  2. localStorage.removeItem('myItem'): To remove individual items

The Answer 6

7 people think this answer is useful

If you want to clear all item you stored in localStorage then


Use this for clear all stored key.

If you want to clear/remove only specific key/value then you can use removeItem(key).


The Answer 7

4 people think this answer is useful

First things first, you need to check to make sure that localStorage is enabled. I would recommend doing it like this:

var localStorageEnabled = false;
try { localStorageEnabled = !!localStorage; } catch(e) {};

Yes, you can (in some cases) just check to see if the localStorage is a member of the window object. However, there are iframe sandboxing options (among other things) that will throw an exception if you even attempt to access the index ‘localStorage’. Thus, for best-practices reasons, this is the best way to check to see if the localStorage is enabled. Then, you can just clear the localStorage like so.

if (localStorageEnabled) localStorage.clear();

For example, you could clear the localStorage after an error occurs in webkit browsers like so.

// clears the local storage upon error
if (localStorageEnabled)
  window.onerror = localStorage.clear.bind(localStorage);

In the above example, you need the .bind(window) because without it, the localStorage.clear function will run in the context of the window object, instead of the localStorage object making it silently fail. To demonstrate this, look at the below example:

window.onerror = localStorage.clear;

is the same as:

window.onerror = function(){

The Answer 8

4 people think this answer is useful



to clear particular item


To remove particular value by id :

var item_detail = JSON.parse(localStorage.getItem("key_name")) || [];           
            $.each(item_detail, function(index, obj){
                if (key_id == data('key')) {
                    localStorage["key_name"] = JSON.stringify(item_detail);
                    return false;

The Answer 9

1 people think this answer is useful

To clear sessionStorage


The Answer 10

1 people think this answer is useful

This code here you give a list of strings of keys that you don’t want to delete, then it filters those from all the keys in local storage then deletes the others.

const allKeys = Object.keys(localStorage);

const toBeDeleted = allKeys.filter(value => {
  return !this.doNotDeleteList.includes(value);

toBeDeleted.forEach(value => {

The Answer 11

0 people think this answer is useful

Here is a simple code that will clear localstorage stored in your browser by using javascript

    <script type="text/javascript">

if(localStorage) { // Check if the localStorage object exists

    localStorage.clear()  //clears the localstorage

} else {

    alert("Sorry, no local storage."); //an alert if localstorage is non-existing


To confirm if localstorage is empty use this code:

<script type="text/javascript">

// Check if the localStorage object exists
if(localStorage) {

    alert("Am still here, " + localStorage.getItem("your object name")); //put the object name
} else {
    alert("Sorry, i've been deleted ."); //an alert


if it returns null then your localstorage is cleared.

Add a Comment