As a Gmail user, I’ve enjoyed their service for over a decade now. And never worried about the storage space left in the account as 15GB was generously given, and back in the day, Photos didn’t consume from the free storage quota. Recently, a few months back, I started getting alerts that over 70% of the storage was used. This wasn’t really an alert; it was a promo for a Google One subscription. Then I decided to clean-up old useless mails. This included thousands of notification, promotional mails from Amazon, Food delivery apps, many ecom sites, unopened news letters.. etc. a lot actually. I had filtered 20k+ mails manually and deleted them. Then I started finding it difficult to delete the mails. Because I didn’t want to delete some emails with attachments like order receipt, payment copy etc.
Then I came across the best part of Google Apps, the Apps Script. I grabbed a few samples from ChatGPT and modified them for my use case, which gets all the senders in the mailbox and saves them into a spreadsheet. Then I sorted by mail count of sender, and after that I did selective delete. So, somehow, I dealt with the old emails. But what about new emails? Who will do this housekeeping work again after a few months or a year?
So I decided to write a script that would delete all the mail with the label ‘Delete’ and scheduled this script to run every 15 days. I also created a filter that would add the label to mails from certain senders, which I’d have to keep on updating as new sender emails pop up. Here are some script snippets I’m using.
function sender_list1() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Emails")
var tc = 0;
var pageToken = sh.getRange(sh.getLastRow(), 1).getValue() +1;
Logger.log("pageToken: "+pageToken);
var threads;
do {
threads = GmailApp.search('', pageToken, 500);
if (threads && threads.length > 0) {
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = GmailApp.getMessagesForThread(thread);
var data = [];
for (var j = 0; j < messages.length; j++) {
var sender = messages[j].getFrom();
let temp = sender.match(/<(.+?)>/);
var senderEmail = temp != null ? temp[1] : sender;
data.push([pageToken+i, thread.getId(), senderEmail, '1'])
tc++;
}
sh.getRange(sh.getLastRow()+1, 1, data.length, 4).setValues(data);
}
}
pageToken = pageToken + threads.length;
Logger.log("Next pageToken: "+pageToken);
Logger.log("tc: "+tc);
} while (threads.length > 0);
Logger.log("tc: "+tc);
}
function autoDelete() {
var test = GmailApp.getAliases();
console.log('Started autoDelete run.');
var delayDays = 15;
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("Delete");
var threads = label.getThreads();
if(threads.length > 0){
console.log('Found ' + threads.length + ' emails marked for deletion.');
var counter = 0;
try {
for(var i =0; i < threads.length; i++){
if (threads[i].getLastMessageDate()<maxDate){
Logger.log('deleted email: ' + threads[i].getFirstMessageSubject());
// threads[i].markUnimportant();
// threads[i].markRead();
threads[i].moveToTrash();
counter++;
}
}
console.log('Successfully moved ' + counter + 'emails to the trash.');
}
catch(e){
console.error('Could Not Start Run: ' + e);
}
}
else{
console.log('Found ' + threads.length + 'emails marked for deletion. Exiting.');
}
}