Skip to content
Latest: Publish JavaScript Packages to NPM Like a Pro!

clear() Storage Method – How to Empty Web Storage

The clear() method clears (deletes) all the items in the specified web storage.

clear() accepts no argument. Here is the syntax:

webStorageObject.clear();

webStorageObject represents the storage object whose items you wish to clear—that is, localStorage or sessionStorage.

Below are examples of the clear() method.

How to clear all items from session storage

Section titled “How to clear all items from session storage”

Invoke sessionStorage’s clear() method.

// Store carColor: "Pink" in the session storage:
sessionStorage.setItem("carColor", "Pink");
// Store pcColor: "Yellow" in the session storage:
sessionStorage.setItem("pcColor", "Yellow");
// Store laptopColor: "White" in the session storage:
sessionStorage.setItem("laptopColor", "White");
// Clear all items from the session storage:
sessionStorage.clear();
// Confirm whether the session storage still contains any item:
console.log(sessionStorage);
// The invocation above will return: {length: 0}

Try Editing It

Invoke localStorage’s clear() method.

// Store carColor: "Pink" in the local storage:
localStorage.setItem("carColor", "Pink");
// Store pcColor: "Yellow" in the local storage:
localStorage.setItem("pcColor", "Yellow");
// Store laptopColor: "White" in the local storage:
localStorage.setItem("laptopColor", "White");
// Clear all items from the local storage:
localStorage.clear();
// Confirm whether the local storage still contains any item:
console.log(localStorage);
// The invocation above will return: {length: 0}

Try Editing It