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

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

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

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.

Below are examples of the removeItem() method.

  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

How to remove data from the local storage object

Section titled “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