-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode.js
149 lines (132 loc) · 3.74 KB
/
code.js
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
/**
*
*
* This Google Script will delete everything in your Gmail account.
* It removes email messages, filters, labels and reset all your settings
*
* Written by Amit Agarwal (amit@labnol.org)
88
88
88
,adPPYb,88 ,adPPYYba, 8b,dPPYba, ,adPPYb,d8 ,adPPYba, 8b,dPPYba,
a8" `Y88 "" `Y8 88P' `"8a a8" `Y88 a8P_____88 88P' "Y8
8b 88 ,adPPPPP88 88 88 8b 88 8PP""""""" 88
"8a, ,d88 88, ,88 88 88 "8a, ,d88 "8b, ,aa 88
`"8bbdP"Y8 `"8bbdP"Y8 88 88 `"YbbdP"Y8 `"Ybbd8"' 88
aa, ,88
"Y8bbdP"
* Proceed with great caution since the process is irreversible
*
* This software comes with ABSOLUTELY NO WARRANTY.
* This is free software, and you are welcome to modify and redistribute it
*
* This permission notice shall be included in all copies of the Software.
*
*
*/
/**
* Remove all labels in Gmail
*/
const deleteGmailLabels_ = () => {
GmailApp.getUserLabels().forEach((label) => {
label.deleteLabel();
});
};
/**
* Remove all Gmail Filters
*/
const deleteGmailFilters_ = () => {
const { filter: gmailFilters } = Gmail.Users.Settings.Filters.list('me');
gmailFilters.forEach(({ id }) => {
Gmail.Users.Settings.Filters.remove('me', id);
});
};
/**
* Remove all Gmail Draft messages
*/
const deleteGmailDrafts_ = () => {
GmailApp.getDrafts().forEach((draft) => {
draft.deleteDraft();
});
};
/**
* Reset Gmail Settings
*/
const resetGmailSettings_ = () => {
// Disable Out-of-office
Gmail.Users.Settings.updateVacation({ enableAutoReply: false }, 'me');
// Delete Gmail Signatures
const { sendAs } = Gmail.Users.Settings.SendAs.list('me');
sendAs.forEach(({ sendAsEmail }) => {
Gmail.Users.Settings.SendAs.update({ signature: '' }, 'me', sendAsEmail);
});
// Disable IMAP
Gmail.Users.Settings.updateImap({ enabled: false }, 'me');
// Disable POP
Gmail.Users.Settings.updatePop({ accessWindow: 'disabled' }, 'me');
// Disable Auto Forwarding
const { forwardingAddresses } = Gmail.Users.Settings.ForwardingAddresses.list('me');
forwardingAddresses.forEach(({ forwardingEmail }) => {
Gmail.Users.Settings.ForwardingAddresses.remove('me', forwardingEmail);
});
};
const startTime = Date.now();
const isTimeLeft_ = () => {
const ONE_SECOND = 1000;
const MAX_EXECUTION_TIME = ONE_SECOND * 60 * 5;
return MAX_EXECUTION_TIME > Date.now() - startTime;
};
/**
* Move all Gmail threads to trash folder
*/
const deleteGmailThreads_ = () => {
let threads = [];
do {
threads = GmailApp.search('in:all', 0, 100);
if (threads.length > 0) {
GmailApp.moveThreadsToTrash(threads);
Utilities.sleep(1000);
}
} while (threads.length && isTimeLeft_());
};
/**
* Move all Spam email messages to the Gmail Recyle bin
*/
const deleteSpamEmails_ = () => {
let threads = [];
do {
threads = GmailApp.getSpamThreads(0, 10);
if (threads.length > 0) {
GmailApp.moveThreadsToTrash(threads);
Utilities.sleep(1000);
}
} while (threads.length && isTimeLeft_());
};
/**
* Permanetly empty the Trash folder
*/
const emptyGmailTrash_ = () => {
let threads = [];
do {
threads = GmailApp.getTrashThreads(0, 100);
threads.forEach((thread) => {
Gmail.Users.Threads.remove('me', thread.getId());
});
} while (threads.length && isTimeLeft_());
};
/**
* Factory Reset your Gmail Account
* Replace NO with YES and run this function
* */
const factoryResetGmail = () => {
const FACTORY_RESET = 'NO';
if (FACTORY_RESET === 'YES') {
resetGmailSettings_();
deleteGmailLabels_();
deleteGmailFilters_();
deleteGmailDrafts_();
deleteGmailThreads_();
deleteSpamEmails_();
emptyGmailTrash_();
}
};