Looking for a script to delete emails from a specific sender from trash

404 views
Skip to first unread message

Dave Powell

unread,
Sep 22, 2019, 7:09:12 PM9/22/19
to Google Apps Script Community
Hi there, I'm looking to completely and permanently delete emails from a specific sender within gmail. Looking up ways to solve this problem came upon scripts as a potential solution. I have not used scripts but can follow instructions well enough.

Requirements are:
  • Script that permanently deletes emails from trash if they are from a specific sender.
  • That the script run on a schedule as frequently as possible.
The ideal result is that I'm unaware the email arrived in the first place.

Thank you, let me know if you need further questions.

ChiefChippy2

unread,
Sep 23, 2019, 4:43:12 AM9/23/19
to Google Apps Script Community
I don't think this needs google apps script, try to create a GMail filter matching the sender's email, then select "Delete it" as the action.

Dave Powell

unread,
Sep 23, 2019, 10:35:50 AM9/23/19
to google-apps-sc...@googlegroups.com
That only sends it to the trash where it's still visible to the user (I'm helping someone else with this). What we're looking for is a way for the email to be entirely removed from gmail without the user being aware an email was received in the first place.

On Mon, Sep 23, 2019 at 2:43 AM ChiefChippy2 <soloui...@gmail.com> wrote:
I don't think this needs google apps script, try to create a GMail filter matching the sender's email, then select "Delete it" as the action.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/93c5782e-e78d-4bad-afe8-73c0860ed1d7%40googlegroups.com.

Clark Lind

unread,
Sep 23, 2019, 12:28:42 PM9/23/19
to Google Apps Script Community
I don't know if Apps script can do more than move a message or thread to Trash or Spam. I don't know of any method to "Empty Trash".

On Monday, September 23, 2019 at 10:35:50 AM UTC-4, Dave Powell wrote:
That only sends it to the trash where it's still visible to the user (I'm helping someone else with this). What we're looking for is a way for the email to be entirely removed from gmail without the user being aware an email was received in the first place.

On Mon, Sep 23, 2019 at 2:43 AM ChiefChippy2 <soloui...@gmail.com> wrote:
I don't think this needs google apps script, try to create a GMail filter matching the sender's email, then select "Delete it" as the action.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

ChiefChippy2

unread,
Sep 23, 2019, 12:51:06 PM9/23/19
to google-apps-sc...@googlegroups.com
Advanced Gmail Service can permanently delete it.
Apply the filter and the code below ( add a time trigger and authorize it correctly : https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services )
function example(){
var NotWanted= GmailApp.search("is:trash from:em...@em.ail")
var arrayId=[]
for(var i =0;i<=NotWanted.length;i++){

arrayId.push(NotWanted[i].getId())
}
Gmail.Users.Messages.batchDelete(arrayId,"me")
}

(Try this code there might be an error or two )
Edited ( I changed the code a tiny bit )

Dave Powell

unread,
Sep 23, 2019, 3:27:25 PM9/23/19
to google-apps-sc...@googlegroups.com
Thanks! It ran, requested permissions, and then dumped this error. Using the email from your example for now.

TypeError: Cannot call method "getID" of unidentified. (line 6, file "Code")

Running the debug I get this:

image.png

On Mon, Sep 23, 2019 at 10:51 AM ChiefChippy2 <soloui...@gmail.com> wrote:
Advanced Gmail Service can permanently delete it.
Apply the filter and the code below ( add a time trigger and authorize it correctly )
function example(){
var NotWanted= GmailApp.search("is:trash from:em...@em.ail")
var arrayId=[]
for(var i =0;i<=NotWanted.length;i++){

arrayId.push(NotWanted[i].getId())
}
UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/messages/batchDelete",{"method":"POST","payload":{"id":arrayId}})
}

(Try this code there might be an error or two )
On Monday, September 23, 2019 at 6:28:42 PM UTC+2, cwlind wrote:
I don't know if Apps script can do more than move a message or thread to Trash or Spam. I don't know of any method to "Empty Trash".

On Monday, September 23, 2019 at 10:35:50 AM UTC-4, Dave Powell wrote:
That only sends it to the trash where it's still visible to the user (I'm helping someone else with this). What we're looking for is a way for the email to be entirely removed from gmail without the user being aware an email was received in the first place.

On Mon, Sep 23, 2019 at 2:43 AM ChiefChippy2 <soloui...@gmail.com> wrote:
I don't think this needs google apps script, try to create a GMail filter matching the sender's email, then select "Delete it" as the action.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/57c035e7-0d95-4221-8e98-9a794b3d9526%40googlegroups.com.

ChiefChippy2

unread,
Sep 23, 2019, 4:19:46 PM9/23/19
to google-apps-sc...@googlegroups.com
1.) I didn't mention but you should change "em...@em.ail" to the sender's email ;)

2.) I don't know if my edited post has been saved but try this code - 
Advanced Gmail Service can permanently delete it.
Apply the filter and the code below ( add a time trigger and authorize it correctly : https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services )
function example(){
var NotWanted= GmailApp.search("is:trash from:em...@em.ail")
//Change the bolded email address to the real one.
var arrayId=[]
for(var i =0;i<NotWanted.length;i++){

arrayId.push(NotWanted[i].getId())
}
Gmail.Users.Messages.batchDelete(arrayId,"me")
}

(Try this code there might be an error or two )
Edited ( I changed the code a tiny bit )
- show quoted text -



On Monday, September 23, 2019 at 9:27:25 PM UTC+2, Dave Powell wrote:
Thanks! It ran, requested permissions, and then dumped this error. Using the email from your example for now.

TypeError: Cannot call method "getID" of unidentified. (line 6, file "Code")

Running the debug I get this:

image.png

On Mon, Sep 23, 2019 at 10:51 AM ChiefChippy2 <soloui...@gmail.com> wrote:
Advanced Gmail Service can permanently delete it.
Apply the filter and the code below ( add a time trigger and authorize it correctly )
function example(){
var NotWanted= GmailApp.search("is:trash from:em...@em.ail")
var arrayId=[]
for(var i =0;i<=NotWanted.length;i++){

arrayId.push(NotWanted[i].getId())
}
UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/messages/batchDelete",{"method":"POST","payload":{"id":arrayId}})
}

(Try this code there might be an error or two )

On Monday, September 23, 2019 at 6:28:42 PM UTC+2, cwlind wrote:
I don't know if Apps script can do more than move a message or thread to Trash or Spam. I don't know of any method to "Empty Trash".

On Monday, September 23, 2019 at 10:35:50 AM UTC-4, Dave Powell wrote:
That only sends it to the trash where it's still visible to the user (I'm helping someone else with this). What we're looking for is a way for the email to be entirely removed from gmail without the user being aware an email was received in the first place.

On Mon, Sep 23, 2019 at 2:43 AM ChiefChippy2 <soloui...@gmail.com> wrote:
I don't think this needs google apps script, try to create a GMail filter matching the sender's email, then select "Delete it" as the action.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

ChiefChippy2

unread,
Sep 23, 2019, 4:27:12 PM9/23/19
to Google Apps Script Community
Also - the error was caused by the send query not returning any thing and the for loop running 1 more time than necessary ( see my previous post for the correct code, if you are seeing this from your email, please copy-paste the code from the web version of the group chat since I often edit my code afterwards )
Reply all
Reply to author
Forward
0 new messages