Web Storage: clear()
The clear() method removes every key and value from one storage area for the current origin.
What you will learn
- Which values
clear()removes - When to use
removeItem()instead - How to explain a destructive reset to users
sessionStorage.clear();This affects all values in that storage area, not only the values owned by one feature. Prefer removeItem() when you need to delete one key.
Common mistakes
- Do not call
clear()when unrelated features share the storage area. - Tell users when an action removes saved preferences or drafts.
- Handle storage errors and keep a usable default.