Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Storage.clear() — removing all entries

Storage.clear() synchronously removes every key–value pair from the selected Web Storage area. It takes no arguments and returns undefined. Be careful: it also deletes entries unrelated to your code.

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.clear()

Storage.clear operates on a Storage object: localStorage.clear() clears the origin's local area, while sessionStorage.clear() clears the session area for that origin and tab. It does not clear both at once. This is an HTML-standard API, not ECMAScript.

The method is synchronous, takes no arguments, and returns undefined. An empty area stays empty. To delete one entry, use removeItem(key); clear() does not filter keys by prefix.

Removing entries queues a storage event for other eligible documents sharing the area, not the calling document. Its key, oldValue, and newValue fields are null. An empty area produces no event.

Accessing window.localStorage or window.sessionStorage can throw SecurityError, for example because of browser policy. In the example, sandboxing gives the iframe an opaque origin. typeof Storage.prototype.clear returns "function", but reading window.sessionStorage throws SecurityError before the code calls clear(). No entries are deleted. To test a successful sessionStorage.clear() call, use only a disposable test tab and origin: it deletes every entry in the session storage area, including earlier entries.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Storage.clear() and storage access</title>
</head>
<body>
  <script>
    const frame = document.createElement('iframe');
    frame.title = 'Isolated Storage.clear test';
    frame.setAttribute('sandbox', 'allow-scripts');
    frame.srcdoc = `<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Storage clear test</title></head>
<body>
  <p role="status"></p>
  <script>
    const methodType = typeof Storage.prototype.clear;
    const methodStatus = 'typeof Storage.prototype.clear: ' + methodType;
    let resultStatus;
    try {
      const storage = window.sessionStorage;
      storage.setItem('demo:color', 'blue');
      storage.setItem('demo:count', '2');
      const result = storage.clear();
      resultStatus = 'Remaining entries: ' + storage.length + '; return value: ' + String(result);
    } catch (error) {
      resultStatus = 'Clear not completed: ' + error.name;
    }
    document.querySelector('[role="status"]').textContent = methodStatus + '. ' + resultStatus;
    console.log(methodStatus);
    console.log(resultStatus);
  <\/script>
</body>
</html>`;
    document.body.append(frame);
  </script>
</body>
</html>

Result

typeof Storage.prototype.clear: function
Clear not completed: SecurityError

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