.clear()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Aug 2, 2023
Contribute to Docs

.clear() removes all keys in a Storage object.

Syntax

Storage.clear()

The method does not accept arguments. It will clear all storage keys and their respective values.

Example

In this example, new items are added to sessionStorage and localStorage from console. The .clear() method then removes all data.

// Add items to session storage.
sessionStorage.setItem('tab-item1', 'TI-1');
sessionStorage.setItem('tab-item2', 'TI-2');
// Add items to local storage.
localStorage.setItem('domain-item1', 'DI-1');
localStorage.setItem('domain-item2', 'DI-2');
// Remove all items from both local and session storage.
sessionStorage.clear();
localStorage.clear();
// Output storage.
console.log(sessionStorage.length);
console.log(localStorage.length);

This example results in the following output:

0
0

All contributors

Looking to contribute?

Learn JavaScript on Codecademy