-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
167 lines (100 loc) · 2.71 KB
/
Code.gs
File metadata and controls
167 lines (100 loc) · 2.71 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
/*
Use this config to custom your labels and pruge schedule
Yon can add more rules and change the search for label, category.
Example:
var config = [
{
search: "category:promotions",
purge_after: "10" // day
},
{
search: "label:Useless",
purge_after: "10"
}
];
*/
var config = [
{
search: "label:deleted-after-10-days",
purge_after: "10" // day
},
{
search: "label:deleted-after-30-days",
purge_after: "30" // day
},
{
search: "label:deleted-after-90-days",
purge_after: "90" // day
}
];
/*
Edited by @Jorand for multi rules
For details, refer http://labnol.org/?p=27605
T U T O R I A L
- - - - - - - -
Step 1. Update the values of fields GMAIL_LABEL and PURGE_AFTER above.
Step 2. Go to Run -> Initialize and authorize the script.
Step 3. Go to Run -> Install to install the script.
You can now exit this window and any email messages in Gmail folder will automatically
get purged after 'n' days. The script will run by itself everyday at 01:00 hours.
Also, you may go to Run -> Uninstall to stop the purging script anytime.
example config :
var config = [
{
search: "category:promotions", // label, category
purge_after: "10" // day
},
{
search: "category:forums",
purge_after: "90"
},
{
search: "label:Useless",
purge_after: "10"
}
];
*/
function Intialize() {
return;
}
function Install() {
ScriptApp.newTrigger("purgeGmail")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
ScriptApp.newTrigger("purgeGmail")
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function purgeGmail() {
for (var ii=0; ii<config.length; ii++) {
var rule = config[ii];
var age = new Date();
age.setDate(age.getDate() - rule.purge_after);
var purge = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
var search = rule.search + " before:" + purge;
try {
var threads = GmailApp.search(search, 0, 100);
if (threads.length == 100) {
ScriptApp.newTrigger("purgeGmail")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*10))
.create();
}
for (var i=0; i<threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j=0; j<messages.length; j++) {
var email = messages[j];
if (email.getDate() < age) {
email.moveToTrash();
}
}
}
} catch (e) {}
}
}