Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Storage.removeItem — removing one entry

The removeItem(key) method removes a specific key–value pair from localStorage or sessionStorage. It returns undefined; a missing key does not cause an error.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

storage.removeItem(key)

Storage.removeItem is a synchronous Web Storage method defined by the HTML Standard, not ECMAScript. Call it on localStorage or sessionStorage, passing the key as a string. All major browser engines support it, but not every browser engine implements Web Storage. Node 24 being present in a repository does not establish browser support.

removeItem(key) removes only the specified entry and returns undefined, including when the key does not exist. After removal, getItem(key) returns null. By contrast, clear() removes every entry from the selected storage area.

An actual removal queues a storage event for other documents sharing the storage area, not the window performing the operation; newValue is null. A missing key produces no event.

Accessing window.localStorage or window.sessionStorage can throw SecurityError, for example because of browser policy. The example first tries window.sessionStorage. If that attempt results in SecurityError or QuotaExceededError, it uses an in-memory test fixture backed by a Map, not Web Storage. Either way, it chooses an unused key, writes a temporary entry under that key, and then removes the entry.

Examples

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storage.removeItem: removing one entry</title>
</head>
<body>
<h1>Removing a storage entry</h1>
<p id="source"></p>
<script>
  function checkRemoval(storage) {
    let key = 'cktis:removeItem';
    while (storage.getItem(key) !== null) key += ':';

    try {
      storage.setItem(key, 'draft');
      const removed = storage.removeItem(key);
      const missing = storage.getItem(key) === null;
      const removedAgain = storage.removeItem(key);
      return [removed === undefined, missing, removedAgain === undefined && storage.getItem(key) === null];
    } finally {
      storage.removeItem(key);
    }
  }

  let lines;
  let source = 'Using browser sessionStorage.';
  try {
    lines = checkRemoval(window.sessionStorage);
  } catch (error) {
    if (error.name !== 'SecurityError' && error.name !== 'QuotaExceededError') throw error;
    source = 'Could not access browser sessionStorage or write the test entry. Using an in-memory fixture, not Web Storage.';
    const entries = new Map();
    lines = checkRemoval({
      getItem(key) { return entries.has(key) ? entries.get(key) : null; },
      setItem(key, value) { entries.set(key, String(value)); },
      removeItem(key) { entries.delete(key); }
    });
  }
  document.getElementById('source').textContent = source;
  for (const line of lines) console.log(line);
</script>
</body>
</html>

Result

true
true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources