Blog - posts for May 2021

May 03 2021

Delete all emails from one sender Gmail

If you ever wanted to delete emails from single sender via Gmail, you cannot just bulk delete them from an email chain you have to, individually click each email from the center in what Google calls threads aka email chains. Now if you have ever forwarded your email to another email provider and that email became full you might get a postmaster response of unable to deliver email. But there is a thing called script.Google.com. Where you can write your own scripts to do what you need to do. For most people forget, but I decided I was going to create a script function because it is to do one such thing. After looking around on the Internet to try and find a way to delete messages from a single sender I found that a lot of posts are about deleting Gmail threads which is not the same thing. If you want to make sure that your emails are clean and not full of junk, not spend hours deleting. You might just have to start Java Scripting with Google's Gmail API.

function myFunction() {
for (var k = 0 ; k < 20; k++) {
var thread = GmailApp.getInboxThreads(k*500, 500);
//var paged =GmailApp.getp
for (var j = 0 ; j < thread.length; j++) {
var messages = GmailApp.getMessagesForThread(thread[j]);
for (var i = 0 ; i < messages.length; i++) {
if( messages[i].getFrom().includes("postmaster@outlook.com")){
Logger.log("from: " + messages[i].getFrom());
GmailApp.moveMessageToTrash(messages[i]);
}
}
}
}
}

So, Google will give you the function "myFunction" and then in the brackets you are supposed to write your function so if you start to look at the innermost "for" loop it logs where your email message to in this particular situation is forwarding to Hotmail. Hotmail responds postmaster@outlook.com. In this for loop there is an if statement to search for the specific email. If you want to replace the email that you don't want in your email you can easily remove everything between the quotes. Be mindful just because you delete email in your email account does not mean that Google does not have a copy of it or dozens of other people don't also but that's a whole different conversation. Within that statement, we loop through the messages and find the messages that say postmaster@outlook.com we move them to the trash as you can see in my Gmail app. Then above that is a for loop to iterate through the threads. Again, Gmail considers threads to be a chain of emails within the conversation. Above that is, a loop to go through all threads because Google can only handle so many threads at a time so there is a maximum value in Gmail app. The function, getInboxThreads() the second value 500 is the max threads Gmail can handle in this function. So, you loop through about 500 then go through another loop  to 1000. What happened in this situation is my email has thousands and thousands of emails so function cannot handle everything at once. But this puts a lot of emails in your trash. In Gmail and you have to go into your trash in Gmail and empty it. If you do you'll get rid of them forever and clear up space but you can just move them to trash and your inbox looks perfectly okay. Should you grab any old script.google.com from somebody in run it. No! Unless, you have reviewed the code and know exactly what you are doing, don't test code on important email accounts box or non-sandbox email accounts. There are other issues with using scripts.google.com one of which is you must enable permissions to use app scripts. You will get an email from Google saying you have allowed permission which could blah blah blah and have side effects ... you just have to enable it… You can disable it later if you like also you must use the Gmail library with in app scripts. I'm not trying to go into so many details about gmails' scripts, but below you can see my execution log showing logs of emails I did include the empty trash. Hope this was helpful. Also disclaimer I type all this using voice recognition because I worked too much my hands hurt. One finger that works is the one you know I use constantly.scriptthrash.PNGscripttrash.PNGscript.PNG

Tags:
Created by Nicholas Hunter on 2005/01/28 15:50