Skip to main content

removeItem() Method – How to Remove Item from Web Storage

The removeItem() method removes a property from the specified web storage.

Syntax of the removeItem() Method

removeItem() accepts one required argument. Here is the syntax:

webStorageObject.removeItem(key);
  • webStorageObject represents the storage object whose item you wish to remove—that is, localStorage or sessionStorage.
  • key is the only argument accepted by removeItem(). It is a required string argument specifying the name of the web storage property you want to remove.

Examples

Below are examples of the removeItem() method.

How to remove data from session storage

  1. Invoke sessionStorage's removeItem() method.
  2. Provide the name of the data you wish to remove.
// 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");

// Remove the pcColor item from the session storage:
sessionStorage.removeItem("pcColor");

// Confirm whether the pcColor item still exists in the session storage:
sessionStorage.getItem("pcColor");

// The invocation above will return: null

Try Editing It

info

The removeItem() method will do nothing if its argument does not exist in the session storage.

How to remove data from the local storage object

  1. Invoke localStorage's removeItem() method.
  2. Provide the name of the data you wish to remove.
// 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");

// Remove the pcColor item from the local storage:
localStorage.removeItem("pcColor");

// Confirm whether the pcColor item still exists in the local storage:
localStorage.getItem("pcColor");

// The invocation above will return: null

Try Editing It

info

The removeItem() method will do nothing if its argument does not exist in the local storage.

Buy me a coffee