Failed to execute 'setItem' on 'Storage': quota exceeded, even after clear()
Failed to execute 'setItem' on 'Storage': Setting the value of 'state' exceeded the quota.Short answer
The browser refuses a write because the storage quota is used up. If clear() changes nothing, the value you are writing is probably too big by itself, or you cleared a different storage area or origin. Persist less data, and catch the error instead of letting it crash the app.
Fix it fast
1Find out which cause you have
Measure how large the string you are about to store is. Open the console on the failing page and compare it with what the whole storage holds.
const s = JSON.stringify(store.getState()); console.log("state length:", s.length); console.log("keys in localStorage:", localStorage.length);2Match what you see, then jump to its fix
- 1
The error appears on the same write every time, and localStorage.clear() does not helpThe value you write is larger than the quota on its ownFix - 2
clear() runs without error and the write still fails, or the problem exists only on one URL such as localhost:3000You are clearing a different storage area or a different originFix - 3
The error appears only after the app has been used for a long time, or only with many tabs openSeveral tabs and libraries fill the same shared spaceFix
- 1
What does the error look like?
Failed to execute 'setItem' on 'Storage': Setting the value of 'state' exceeded the quota.QuotaExceededError
Cause 1 of 3
The value you write is larger than the quota on its own
1Check
MDN describes QuotaExceededError as thrown when storage has run out of disk quota or the user declined to grant more space. A single oversized value can fail the write even when few other keys exist. A common cause is persisting the entire Redux tree, including cached API responses or large lists.
console.log(new Blob([JSON.stringify(store.getState())]).size, 'bytes');2Fix
Persist only the slices that must survive a reload, such as a token or a few settings, and keep large lists in memory or on the server. Wrap setItem in try/catch so a full storage never crashes the page.
function saveSettings(state) {
try {
localStorage.setItem("settings", JSON.stringify(state.settings));
} catch (err) {
if (err instanceof DOMException && err.name === "QuotaExceededError") {
console.warn("Storage full, skipping save");
} else {
throw err;
}
}
}3Verify
Reload the page, then repeat the action that used to fail; the write succeeds and the state is small.
console.log(localStorage.getItem('settings')?.length);Confirmed by developer.mozilla.org
Cause 2 of 3
You are clearing a different storage area or a different origin
1Check
sessionStorage and localStorage are separate areas, and each origin has its own pair. Origin means scheme, host and port, so http://localhost:3000 and http://localhost:5173 do not share data. clear() empties only the area and origin it is called on.
console.log(location.origin);
console.log(Object.keys(localStorage), Object.keys(sessionStorage));2Fix
Clear the storage that actually holds the old data, on the origin that fails, and remove only the key you own with removeItem rather than wiping everything.
localStorage.removeItem("state");
// or, for everything on this origin only
localStorage.clear();3Verify
List the keys again, then retry the write.
console.log(Object.keys(localStorage));Confirmed by developer.mozilla.org
Cause 3 of 3
Several tabs and libraries fill the same shared space
1Check
For localStorage, the storage space is shared between all tabs with the same origin. Everything written by any of those tabs counts toward the same limit.
console.table(Object.entries(localStorage).map(([k, v]) => ({ key: k, length: v.length })));2Fix
Remove keys you no longer use with removeItem. If the data only needs to live for the session, use sessionStorage instead.
sessionStorage.setItem("draft", JSON.stringify(draft));3Verify
The table of keys is shorter and the write works.
console.table(Object.entries(localStorage).map(([k, v]) => ({ key: k, length: v.length })));Confirmed by developer.mozilla.org
How do I stop it coming back?
- Persist a whitelist of small slices, never the whole store.
- Always wrap storage writes in try/catch; they can fail for quota reasons.
- Store only strings: use JSON.stringify and JSON.parse on the way in and out.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix