Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Failed to execute 'setItem' on 'Storage': quota exceeded, even after clear()

ErrorFailed 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.

  • Applies to Chrome, Firefox, React 19, Redux Toolkit 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2clear() 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. 3The 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

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.