-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailCleanup.gs
More file actions
64 lines (50 loc) · 1.4 KB
/
mailCleanup.gs
File metadata and controls
64 lines (50 loc) · 1.4 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
const process_limit = 100;
const max_tries = 100;
function mailCleanup() {
archive_opened(10);
trash_ndays(30);
trash_ndays(60);
trash_ndays(90);
}
function trash_ndays(days) {
const query = 'NOT in:inbox label:trash' + days + ' older_than:' + days + 'd';
let total = 0;
let i;
for (i = 0; i < max_tries; i++) {
let mails = GmailApp.search(query, 0, process_limit);
let n = mails.length;
if (n <= 0)
break;
Logger.log("trash" + days + ": trash " + n + " mails");
GmailApp.moveThreadsToTrash(mails);
}
Logger.log("Trash" + days + ": " + total + " mails, (loop count=" + (i + 1) + ")");
}
function archive_opened(days) {
const d = new Date
const year = d.getFullYear();
let query = 'in:inbox is:read';
if (days > 0)
query += ' older_than:' + days + 'd';
let label = GmailApp.getUserLabelByName(year);
if (label == null) {
GmailApp.createLabel(year);
label = GmailApp.getUserLabelByName(year);
if (label == null) {
Logger.log("couldn't create label: " + year);
return;
}
}
var total = 0;
for (let i = 0; i < max_tries; i++) {
let mails = GmailApp.search(query, 0, process_limit);
let n = mails.length;
if (n <= 0)
break;
Logger.log("archived " + n + " mails");
label.addToThreads(mails);
GmailApp.moveThreadsToArchive(mails);
total += n;
}
Logger.log("archived " + total + " mails");
}