.clear()
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:
00
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.