Storage Quota Management in JavaScript

Check and manage storage quota

IntermediateLocalStorage/SessionStorageExample 5 of 10
storage-quota-management.js
1// Method 1: Check storage quota
2async function getStorageQuota() {
3 if ('storage' in navigator && 'estimate' in navigator.storage) {
4 const estimate = await navigator.storage.estimate();
5 console.log('Quota:', estimate.quota);
6 console.log('Usage:', estimate.usage);
7 console.log('Available:', estimate.quota - estimate.usage);
8 console.log('Usage %:', ((estimate.usage / estimate.quota) * 100).toFixed(2) + '%');
9 return estimate;
10 }
11 return null;
12}
13
14getStorageQuota();
15
16// Method 2: Check localStorage size
17function getLocalStorageSize() {
18 let total = 0;
19 for (let key in localStorage) {
20 if (localStorage.hasOwnProperty(key)) {
21 total += localStorage[key].length + key.length;
22 }
23 }
24 return total;
25}
26
27const size = getLocalStorageSize();
28console.log('LocalStorage size:', size, 'bytes');
29console.log('Size in KB:', (size / 1024).toFixed(2));
30
31// Method 3: Check if storage is full
32function isStorageFull() {
33 try {
34 const test = '__storage_test__';
35 localStorage.setItem(test, 'test');
36 localStorage.removeItem(test);
37 return false;
38 } catch (e) {
39 return e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED';
40 }
41}
42
43if (isStorageFull()) {
44 console.log('Storage is full');
45} else {
46 console.log('Storage has space');
47}
48
49// Method 4: Clear old data when full
50function setItemWithCleanup(key, value) {
51 try {
52 localStorage.setItem(key, value);
53 } catch (e) {
54 if (e.name === 'QuotaExceededError') {
55 console.log('Storage full, clearing old data...');
56 // Clear items older than 7 days
57 clearOldItems(7);
58 // Try again
59 localStorage.setItem(key, value);
60 } else {
61 throw e;
62 }
63 }
64}
65
66function clearOldItems(days) {
67 const cutoff = Date.now() - (days * 24 * 60 * 60 * 1000);
68 for (let i = localStorage.length - 1; i >= 0; i--) {
69 const key = localStorage.key(i);
70 const item = localStorage.getItem(key);
71 try {
72 const data = JSON.parse(item);
73 if (data.timestamp && data.timestamp < cutoff) {
74 localStorage.removeItem(key);
75 }
76 } catch (e) {
77 // Not JSON, skip
78 }
79 }
80}
81
82// Method 5: Monitor storage usage
83function monitorStorage() {
84 setInterval(async () => {
85 const estimate = await navigator.storage.estimate();
86 const usagePercent = (estimate.usage / estimate.quota) * 100;
87
88 if (usagePercent > 80) {
89 console.warn('Storage usage high:', usagePercent.toFixed(2) + '%');
90 }
91 }, 60000); // Check every minute
92}
93
94// Method 6: Request persistent storage
95async function requestPersistentStorage() {
96 if ('storage' in navigator && 'persist' in navigator.storage) {
97 const isPersistent = await navigator.storage.persist();
98 console.log('Persistent storage granted:', isPersistent);
99 return isPersistent;
100 }
101 return false;
102}
103
104requestPersistentStorage();

Output

Quota: 10737418240
Usage: 1024000
Available: 10736394240
Usage %: 0.01%
LocalStorage size: 5120 bytes
Size in KB: 5.00
Storage has space
Persistent storage granted: true

What's going on

Quota management prevents storage errors.

Storage Quota

~5-10MB for localStorage
Varies by browser
Check with estimate()

QuotaExceededError

Thrown when full
Handle gracefully
Clear old data

Monitoring

Check usage regularly
Warn when high
Clean up automatically

Persistent Storage

Request permission
Prevents eviction
Better for important data

Best Practices

Monitor usage
Clean old data
Handle errors
Request persistence if needed