Lingua-e
JavaScript

What is a localStorage Quota Exceeded?

← Errors

A QuotaExceededError (DOMException) is thrown when a write to localStorage or sessionStorage would exceed the browser's storage limit, typically around 5 MB per origin. The error name varies slightly across browsers.

Why it happens

  • Storing large JSON objects or base64-encoded images in localStorage.
  • Accumulating many small items over time that together exceed the quota.
  • Multiple tabs or the same app over time filling up the storage.

Examples

JavaScript

Code that triggers the error

const bigData = "x".repeat(10 * 1024 * 1024); // 10 MB
localStorage.setItem("cache", bigData);

Error output

DOMException: Failed to execute 'setItem' on 'Storage': Setting the value of 'cache' exceeded the quota.

How to fix it

Wrap localStorage.setItem() in a try/catch for QuotaExceededError. Prune old or large items before writing. For larger data, use IndexedDB which has no hard size limit. Consider a cache eviction strategy (LRU, TTL) to keep storage size bounded.

Fixed code

function safeSetItem(key, value) {
  try {
    localStorage.setItem(key, value);
  } catch (e) {
    if (e.name === "QuotaExceededError") {
      localStorage.clear();
      localStorage.setItem(key, value);
    }
  }
}

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free