localStorage Quota Exceeded
Quick summary
Your localStorage setItem call fails because the storage limit (usually 5-10MB) has been reached.
Why this happens
Too much data stored in localStorage for the origin
Large objects stored without compression or pruning
Old data not being cleaned up, accumulating over time
Minimal example
localStorage.setItem("data", largeJSONString);
// QuotaExceededError if storage is fulltry {
localStorage.setItem("data", largeJSONString);
} catch (e) {
if (e.name === "QuotaExceededError") {
// Remove old data and retry
localStorage.removeItem("oldData");
localStorage.setItem("data", largeJSONString);
}
}Wrap setItem in try/catch to handle quota errors. When the quota is exceeded, remove old or unnecessary data and retry, or inform the user.
How to diagnose
Check the error: QuotaExceededError or 'The quota has been exceeded'
Measure current storage usage with the Storage API (navigator.storage.estimate())
Review what data is being stored and whether it can be reduced
How to fix
Remove old or unnecessary data before storing new data
Store only essential data — use a server-side database for large data
Compress data before storing (e.g., shorten keys, remove redundant fields)
Use IndexedDB for larger client-side storage needs
How to prevent
Wrap setItem in try/catch to handle quota errors gracefully
Implement a data cleanup strategy for old entries
Use IndexedDB for data larger than a few MB
Related Resources
Related Glossary
Related Lessons
- Web Storage
Learn web storage.