-
Notifications
You must be signed in to change notification settings - Fork 0
/
purgeGmailBySubject
68 lines (47 loc) · 1.48 KB
/
purgeGmailBySubject
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
// The name of the Gmail Label that is to be purged?
var GMAIL_SUBJECT = "flexmls Web, New Registration";
// Purge messages automatically after how many days?
var PURGE_AFTER = "0";
// Function
function Intialize() {
return;
}
// Installs time triggers.
function Install() {
ScriptApp.newTrigger("purgeGmailsubject")
.timeBased()
.at(new Date((new Date()).getTime() + 1000*60*2))
.create();
ScriptApp.newTrigger("purgeGmailsubject")
.timeBased().everyDays(1).create();
}
function Uninstall() {
var triggers = ScriptApp.getScriptTriggers();
for (var i=0; i<triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function purgeGmailsubject() {
var age = new Date();
age.setDate(age.getDate() - PURGE_AFTER);
var purge = Utilities.formatDate(age, Session.getScriptTimeZone(), "yyyy-MM-dd");
var search = "subject:" + GMAIL_SUBJECT + " before:" + purge;
try {
var threads = GmailApp.search(search, 0, 100);
if (threads.length == 100) {
ScriptApp.newTrigger("purgeGmailsubject")
.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) {}
}