-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_cleanup_script.js
More file actions
610 lines (501 loc) · 21 KB
/
gmail_cleanup_script.js
File metadata and controls
610 lines (501 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Gmail Smart Storage Manager
// Version: 2025.8.4
// Author: Shawn McNeece (@smcneece)
// Description: Simple Gmail storage management using mathematical precision
// GitHub: https://github.com/smcneece/gmail-cleanup-script
// ====================================================================
// CONFIGURATION - EDIT THESE VALUES
// ====================================================================
var REPORT_EMAIL = ''; // Leave blank for auto-detection, or enter 'your-email@example.com'
// Safety Settings
var DRY_RUN = true; // Set to false after testing - ALWAYS TEST FIRST!
var MAX_DELETE_PER_RUN = 2000; // Hard cap per cleanup run (prevents accidental mass deletion)
// Storage Settings
var STORAGE_THRESHOLD = 0.75; // 75% triggers cleanup
// Target Settings
var TARGET_FOLDER = 'in:sent'; // Target these items - sent, trash, inbox, or any folder
// ====================================================================
function smartCleanupCheck() {
// Prevent overlapping runs
var lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) {
console.log('Another cleanup is already running. Exiting.');
return;
}
try {
console.log('Starting smart storage cleanup...');
// Get current storage usage
var storageInfo = getStorageUsage();
var usedGB = storageInfo.usedGB;
var totalGB = storageInfo.totalGB;
var usagePercent = (usedGB / totalGB);
console.log('Current usage: ' + usedGB + 'GB of ' + totalGB + 'GB (' + Math.round(usagePercent * 100) + '%)');
// Only cleanup if over threshold
if (usagePercent > STORAGE_THRESHOLD) {
console.log('Storage over ' + Math.round(STORAGE_THRESHOLD * 100) + '% - calculating cleanup...');
// Calculate exactly how many emails to delete
var cleanupPlan = calculateSmartCleanup(usedGB, totalGB, STORAGE_THRESHOLD, TARGET_FOLDER);
if (cleanupPlan.emailsToDelete > 0) {
var action = DRY_RUN ? 'would delete' : 'deleting';
console.log('Cleanup plan: ' + action + ' ' + cleanupPlan.emailsToDelete + ' emails (~' + cleanupPlan.mbToFree + 'MB) from ' + TARGET_FOLDER);
// Execute the cleanup
var cleanupResult = deleteEmailBatch(TARGET_FOLDER, cleanupPlan.emailsToDelete);
var completed = DRY_RUN ? 'simulated deletion of' : 'deleted';
console.log('Cleanup complete: ' + completed + ' ' + cleanupResult.emailsDeleted + ' emails (~' + cleanupResult.mbFreed + 'MB)');
// Record this cleanup event (only if not dry run)
if (!DRY_RUN) {
recordCleanupEvent(cleanupResult.emailsDeleted, cleanupResult.mbFreed);
}
} else {
console.log('No emails available to delete in target folder');
}
} else {
console.log('Storage under ' + Math.round(STORAGE_THRESHOLD * 100) + '% threshold - no cleanup needed');
}
} catch (error) {
console.error('Error in cleanup:', error);
// Send error notification
try {
var reportEmail = getReportEmail();
GmailApp.sendEmail(reportEmail, 'Gmail Cleanup Error', 'Error during cleanup: ' + error.toString());
} catch (emailError) {
console.error('Could not send error email:', emailError);
}
} finally {
lock.releaseLock();
}
}
function calculateSmartCleanup(usedGB, totalGB, targetThreshold, targetFolder) {
try {
console.log('Calculating cleanup plan...');
// Calculate how much storage we need to free
var currentUsagePercent = usedGB / totalGB;
var storageToFreeGB = (currentUsagePercent - targetThreshold) * totalGB;
var storageToFreeMB = storageToFreeGB * 1024;
console.log('Need to free: ' + storageToFreeGB.toFixed(2) + 'GB (' + Math.round(storageToFreeMB) + 'MB)');
// Sample emails for size calculation
var emailSample = sampleTargetEmails(targetFolder, 200);
if (emailSample.totalEmails === 0) {
console.log('No emails found in target folder');
return { emailsToDelete: 0, mbToFree: 0, averageEmailSize: 0, targetFolderCount: 0 };
}
console.log('Target folder: ' + emailSample.totalEmails + ' emails, average size: ' + emailSample.averageSize.toFixed(2) + 'MB');
// Calculate how many emails to delete
var emailsToDelete = Math.ceil(storageToFreeMB / emailSample.averageSize);
// Apply hard cap
var actualEmailsToDelete = Math.min(emailsToDelete, MAX_DELETE_PER_RUN, emailSample.totalEmails);
console.log('Calculated need: ' + emailsToDelete + ' emails, capped at: ' + actualEmailsToDelete + ' emails');
return {
emailsToDelete: actualEmailsToDelete,
mbToFree: Math.round(actualEmailsToDelete * emailSample.averageSize),
averageEmailSize: emailSample.averageSize,
targetFolderCount: emailSample.totalEmails
};
} catch (error) {
console.error('Error calculating cleanup:', error);
return { emailsToDelete: 0, mbToFree: 0, averageEmailSize: 0, targetFolderCount: 0 };
}
}
function sampleTargetEmails(query, sampleSize) {
try {
console.log('Sampling emails from: ' + query);
// Get total count first
var totalCount = getFullEmailCount(query);
console.log('Found ' + totalCount + ' total emails in target folder');
if (totalCount === 0) {
return { totalEmails: 0, averageSize: 0 };
}
// Sample emails for size calculation using Gmail API for accuracy
var actualSampleSize = Math.min(sampleSize, totalCount);
console.log('Sampling ' + actualSampleSize + ' emails for size calculation...');
var totalSizeBytes = 0;
var sampledCount = 0;
try {
// Use Gmail API to get message IDs
var messageList = Gmail.Users.Messages.list('me', {
q: query,
maxResults: actualSampleSize,
fields: 'messages(id)'
});
if (messageList && messageList.messages) {
for (var i = 0; i < messageList.messages.length; i++) {
var message = messageList.messages[i];
try {
// Get real size using Gmail API
var messageInfo = Gmail.Users.Messages.get('me', message.id, {
format: 'metadata',
fields: 'sizeEstimate'
});
if (messageInfo && messageInfo.sizeEstimate) {
totalSizeBytes += messageInfo.sizeEstimate;
sampledCount++;
}
} catch (msgError) {
console.log('Could not get size for message ' + message.id + ': ' + msgError);
}
// Small delay to be nice to API
if (sampledCount % 10 === 0) {
Utilities.sleep(100);
}
}
}
} catch (apiError) {
console.log('Gmail API sampling failed, using fallback estimation');
// Fallback: use thread-based estimation
var threads = GmailApp.search(query, 0, actualSampleSize);
for (var j = 0; j < threads.length; j++) {
var thread = threads[j];
var messages = thread.getMessages();
// Improved estimation: 100KB base + 50KB per additional message in thread
var estimatedBytes = (100 + (messages.length - 1) * 50) * 1024;
totalSizeBytes += estimatedBytes;
sampledCount++;
}
}
var averageSizeBytes = sampledCount > 0 ? totalSizeBytes / sampledCount : 500 * 1024; // 500KB fallback
var averageSizeMB = averageSizeBytes / (1024 * 1024);
console.log('Size sampling complete: ' + sampledCount + ' emails sampled, average ' + averageSizeMB.toFixed(2) + 'MB');
return {
totalEmails: totalCount,
averageSize: averageSizeMB
};
} catch (error) {
console.error('Error sampling target emails:', error);
return { totalEmails: 0, averageSize: 0.5 }; // 500KB fallback
}
}
function getFullEmailCount(searchQuery) {
try {
// Gmail search has limits, so we need to count in batches
var totalCount = 0;
var hasMore = true;
var start = 0;
var batchSize = 500; // Gmail's max per search
while (hasMore) {
var threads = GmailApp.search(searchQuery, start, batchSize);
totalCount += threads.length;
if (threads.length < batchSize) {
// We got fewer than requested, so we're done
hasMore = false;
} else {
// There might be more, continue with next batch
start += batchSize;
}
// Safety break to avoid infinite loops
if (start > 20000) {
console.log('Hit safety limit at ' + start + ' emails for ' + searchQuery);
break;
}
}
console.log(searchQuery + ': ' + totalCount + ' emails');
return totalCount;
} catch (error) {
console.error('Error counting emails for ' + searchQuery + ':', error);
return 0;
}
}
function deleteEmailBatch(targetFolder, maxEmails) {
var emailsDeleted = 0;
var mbFreed = 0;
try {
var query = targetFolder;
var action = DRY_RUN ? 'simulating deletion of' : 'deleting';
console.log(action + ' up to ' + maxEmails + ' emails from: ' + query);
// Process in batches to respect API limits
var batchSize = 200;
var remaining = maxEmails;
while (remaining > 0 && emailsDeleted < maxEmails) {
var currentBatch = Math.min(remaining, batchSize);
var threads = GmailApp.search(query, 0, currentBatch);
if (threads.length === 0) {
console.log('No more emails found to delete');
break;
}
// Check storage every 400 emails to avoid over-deletion (reduced from every 200)
if (emailsDeleted > 0 && emailsDeleted % 400 === 0) {
try {
var currentStorage = getStorageUsage();
var currentUsagePercent = currentStorage.usedGB / currentStorage.totalGB;
if (currentUsagePercent <= STORAGE_THRESHOLD) {
console.log('Storage threshold reached during cleanup - stopping early');
console.log('Current usage: ' + Math.round(currentUsagePercent * 100) + '%, target: ' + Math.round(STORAGE_THRESHOLD * 100) + '%');
break;
}
} catch (storageCheckError) {
console.log('Could not check storage mid-cleanup, continuing: ' + storageCheckError);
}
}
// Delete this batch
for (var i = 0; i < threads.length; i++) {
if (emailsDeleted >= maxEmails) break;
var thread = threads[i];
try {
// Calculate size for tracking
var threadSizeMB = 0;
if (!DRY_RUN) {
// Get real size for tracking
try {
var threadId = thread.getId();
var threadInfo = Gmail.Users.Threads.get('me', threadId, {
format: 'metadata',
fields: 'messages(sizeEstimate)'
});
if (threadInfo && threadInfo.messages) {
var totalBytes = 0;
for (var j = 0; j < threadInfo.messages.length; j++) {
totalBytes += threadInfo.messages[j].sizeEstimate || 0;
}
threadSizeMB = totalBytes / (1024 * 1024);
}
} catch (sizeError) {
// Fallback estimation
var messages = thread.getMessages();
threadSizeMB = (100 + (messages.length - 1) * 50) / 1024; // KB to MB
}
} else {
// For dry run, use estimation
var messages = thread.getMessages();
threadSizeMB = (100 + (messages.length - 1) * 50) / 1024;
}
mbFreed += threadSizeMB;
// Actually delete (or simulate)
if (!DRY_RUN) {
if (targetFolder === 'in:trash') {
// Permanently delete from trash
var threadId = thread.getId();
Gmail.Users.Threads.remove('me', threadId);
} else {
// Move to trash first, then permanently delete
thread.moveToTrash();
// Wait a moment then permanently delete
Utilities.sleep(500);
var threadId = thread.getId();
Gmail.Users.Threads.remove('me', threadId);
}
}
emailsDeleted++;
} catch (error) {
console.error('Error processing thread:', error);
}
}
remaining -= threads.length;
var status = DRY_RUN ? 'simulated' : 'deleted';
console.log('Batch ' + status + ': ' + threads.length + ' emails, total: ' + emailsDeleted + ', remaining: ' + remaining);
// Delay between batches to be nice to API
if (remaining > 0) {
Utilities.sleep(500);
}
}
var finalAction = DRY_RUN ? 'Simulation complete' : 'Batch deletion complete';
console.log(finalAction + ': ' + emailsDeleted + ' emails processed, ~' + Math.round(mbFreed) + 'MB');
} catch (error) {
console.error('Error during batch processing:', error);
}
return {
emailsDeleted: emailsDeleted,
mbFreed: Math.round(mbFreed)
};
}
function getStorageUsage() {
try {
// Get actual Google account storage usage across all services
var about = Drive.About.get({ fields: 'storageQuota' });
if (about && about.storageQuota) {
var usedBytes = parseInt(about.storageQuota.usage);
var limitBytes = parseInt(about.storageQuota.limit);
var usedGB = usedBytes / (1024 * 1024 * 1024);
var totalGB = limitBytes / (1024 * 1024 * 1024);
console.log('Storage via Drive API: ' + usedGB.toFixed(2) + 'GB of ' + totalGB.toFixed(2) + 'GB');
return {
usedGB: Math.round(usedGB * 100) / 100,
totalGB: Math.round(totalGB * 100) / 100
};
} else {
throw new Error('Storage quota not available');
}
} catch (error) {
console.error('Error getting storage usage:', error);
// Fallback to DriveApp method
try {
var usedBytes = DriveApp.getStorageUsed();
var totalBytes = DriveApp.getStorageLimit();
var usedGB = usedBytes / (1024 * 1024 * 1024);
var totalGB = totalBytes / (1024 * 1024 * 1024);
console.log('Fallback storage: ' + usedGB.toFixed(2) + 'GB of ' + totalGB.toFixed(2) + 'GB');
return {
usedGB: Math.round(usedGB * 100) / 100,
totalGB: Math.round(totalGB * 100) / 100
};
} catch (fallbackError) {
console.error('Both storage methods failed:', fallbackError);
throw new Error('Cannot determine storage usage - aborting cleanup for safety');
}
}
}
function recordCleanupEvent(emailsDeleted, mbFreed) {
try {
var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
var properties = PropertiesService.getScriptProperties();
// Get existing data for today
var existingData = properties.getProperty('cleanup_' + today);
var todayStats = existingData ? JSON.parse(existingData) : { emailsDeleted: 0, mbFreed: 0, runs: 0 };
// Add this run's data
todayStats.emailsDeleted += emailsDeleted;
todayStats.mbFreed += mbFreed;
todayStats.runs += 1;
// Save updated data
properties.setProperty('cleanup_' + today, JSON.stringify(todayStats));
console.log('Recorded cleanup event: ' + emailsDeleted + ' emails, ' + mbFreed + 'MB (daily total: ' + todayStats.emailsDeleted + ' emails)');
} catch (error) {
console.error('Error recording cleanup event:', error);
}
}
function getReportEmail() {
if (REPORT_EMAIL && REPORT_EMAIL.trim() !== '' && REPORT_EMAIL !== 'your-email@example.com') {
return REPORT_EMAIL;
}
// Auto-detect Gmail account email address
try {
var profile = Gmail.Users.getProfile('me');
if (profile && profile.emailAddress) {
console.log('Auto-detected Gmail account: ' + profile.emailAddress);
return profile.emailAddress;
}
} catch (error) {
console.log('Could not auto-detect Gmail address, using fallback');
}
// Fallback to session
try {
var userEmail = Session.getActiveUser().getEmail();
if (userEmail) {
console.log('Using session email: ' + userEmail);
return userEmail;
}
} catch (error) {
console.log('Could not get session email');
}
console.log('Using configured REPORT_EMAIL');
return REPORT_EMAIL;
}
function sendDailyReport() {
try {
var reportEmail = getReportEmail();
var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
// Get current storage
var storageInfo = getStorageUsage();
var usedGB = storageInfo.usedGB;
var totalGB = storageInfo.totalGB;
var freeGB = Math.round((totalGB - usedGB) * 100) / 100;
var usagePercent = (usedGB / totalGB) * 100;
// Get email counts
var emailCounts = countAllEmails();
// Get today's cleanup stats
var properties = PropertiesService.getScriptProperties();
var cleanupData = properties.getProperty('cleanup_' + today);
var todayStats = cleanupData ? JSON.parse(cleanupData) : { emailsDeleted: 0, mbFreed: 0, runs: 0 };
var subject = 'Daily Gmail Storage Report - ' + Math.round(usagePercent) + '% Used' + (DRY_RUN ? ' (DRY RUN MODE)' : '');
var body = 'Daily Storage Report\n';
body += '=====================\n\n';
if (DRY_RUN) {
body += 'WARNING: DRY RUN MODE ACTIVE - No emails are actually deleted\n\n';
}
body += 'Storage Status:\n';
body += '- Used: ' + usedGB + ' GB of ' + totalGB + ' GB\n';
body += '- Free: ' + freeGB + ' GB\n';
body += '- Usage: ' + Math.round(usagePercent) + '%\n\n';
body += 'Email Counts:\n';
body += '- Inbox: ' + emailCounts.inbox + ' emails\n';
body += '- Sent: ' + emailCounts.sent + ' emails\n';
body += '- Trash: ' + emailCounts.trash + ' emails\n';
body += '- Total: ' + emailCounts.total + ' emails\n\n';
if (todayStats.emailsDeleted > 0) {
var action = DRY_RUN ? 'simulated' : 'deleted';
body += 'Cleanup Activity Today:\n';
body += '- Cleanup runs: ' + todayStats.runs + '\n';
body += '- Emails ' + action + ': ' + todayStats.emailsDeleted + '\n';
body += '- Space freed: ~' + todayStats.mbFreed + ' MB\n\n';
} else {
body += 'No cleanup needed today - storage under threshold\n\n';
}
body += 'Configuration:\n';
body += '- Storage threshold: ' + Math.round(STORAGE_THRESHOLD * 100) + '%\n';
body += '- Target folder: ' + TARGET_FOLDER + '\n';
body += '- Max delete per run: ' + MAX_DELETE_PER_RUN + '\n';
body += '- Dry run mode: ' + (DRY_RUN ? 'ON' : 'OFF') + '\n\n';
body += 'Smart cleanup runs automatically when storage exceeds threshold.\n';
body += 'Generated: ' + new Date().toString();
GmailApp.sendEmail(reportEmail, subject, body);
console.log('Daily report sent successfully to: ' + reportEmail);
// Clean up old cleanup data (keep last 7 days)
cleanupOldCleanupData();
} catch (error) {
console.error('Error sending daily report:', error);
}
}
function countAllEmails() {
try {
console.log('Counting all emails (this may take a moment)...');
// Count emails in major folders
var inboxCount = getFullEmailCount('in:inbox');
var sentCount = getFullEmailCount('in:sent');
var trashCount = getFullEmailCount('in:trash');
var spamCount = getFullEmailCount('in:spam');
var counts = {
inbox: inboxCount,
sent: sentCount,
trash: trashCount,
spam: spamCount,
total: inboxCount + sentCount + trashCount + spamCount
};
console.log('Email count complete: ' + counts.total + ' total emails');
return counts;
} catch (error) {
console.error('Error counting emails:', error);
return { inbox: 0, sent: 0, trash: 0, spam: 0, total: 1000 }; // Default fallback
}
}
function cleanupOldCleanupData() {
try {
var properties = PropertiesService.getScriptProperties();
var allProperties = properties.getProperties();
var cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - 7); // Keep 7 days
for (var key in allProperties) {
if (key.indexOf('cleanup_') === 0) {
var dateStr = key.replace('cleanup_', '');
var propDate = new Date(dateStr);
if (propDate < cutoffDate) {
properties.deleteProperty(key);
console.log('Cleaned up old data: ' + key);
}
}
}
} catch (error) {
console.error('Error cleaning up old data:', error);
}
}
// Test Functions
function testSmartCleanup() {
console.log('Running manual cleanup test...');
smartCleanupCheck();
}
function testDailyReport() {
console.log('Running manual daily report test...');
sendDailyReport();
}
function testEmailCount() {
console.log('Running manual email count test...');
var counts = countAllEmails();
console.log('Email counts:', counts);
}
function testDryRun() {
console.log('Testing in dry-run mode...');
console.log('Current DRY_RUN setting: ' + DRY_RUN);
if (!DRY_RUN) {
console.log('WARNING: DRY_RUN is currently FALSE - this would actually delete emails!');
console.log('Set DRY_RUN = true at the top of the script to test safely');
} else {
console.log('DRY_RUN is ON - safe to test');
smartCleanupCheck();
}
}