Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Google Apps Script script to bulk delete large amounts of email in Gmail while avoiding the error #793 which Gmail encounters normally
/*
This script, when used with Google Apps Scripts will delete 500 emails and
can be triggered to run every minute without user interaction enabling you
to bulk delete email in Gmail without getting the #793 error from Gmail.
Configure the search query in the code below to match the type of emails
you want to delete
Browser to https://script.google.com/.
Start a script and paste in the code below.
After you past it in, save it and click the little clock looking button.
This is for your triggers. You can set up how frequently you want the script
to run (I did mine for every minute).
Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ
*/
function batchDeleteEmail() {
var batchSize = 100 // Process up to 100 threads at once
var threads = GmailApp.search('label:inbox from:user@example.com');
for (j = 0; j < threads.length; j+=batchSize) {
GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
}
}
@nnlocke

This comment has been minimized.

Copy link

@nnlocke nnlocke commented Jul 16, 2015

With how common this issue is for people using gmail in the workplace, I'm surprised there aren't more stars here. This works perfectly.

@mcortinas

This comment has been minimized.

Copy link

@mcortinas mcortinas commented Apr 6, 2016

great! it's works fine! thank you very much!

@Faridak

This comment has been minimized.

Copy link

@Faridak Faridak commented Jun 30, 2018

can this batch delete specific email based on time ... or upon forward /print/save action ??

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Jul 17, 2019

@Faridak no, this is just if you want to delete all email in a Gmail account

@Notacoder100

This comment has been minimized.

Copy link

@Notacoder100 Notacoder100 commented Aug 13, 2019

This looks fantastic. Just trying to work out the script to do the following in my gmail:
Delete all emails that are over 2 years old and do not have a label attached to them.

Does anyone know how to do this? Also as you see I am not a programmer but can follow advice.. could you please explain what I would need to do where to achieve this?
Many thanks
Claire

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Aug 13, 2019

@Notacoder100 You can change the search filter on line 18 to define a time window and label state.

@Notacoder100

This comment has been minimized.

Copy link

@Notacoder100 Notacoder100 commented Aug 13, 2019

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Aug 13, 2019

@Notacoder100 Construct a search string in your gmail that matches the emails you want to delete and then use that string in line 18 instead of label:inbox from:user@example.com

@kango88

This comment has been minimized.

Copy link

@kango88 kango88 commented Sep 11, 2019

Thanks for the script! Do you have another version that can do the same but for emptying the trash in gmail?

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Sep 11, 2019

@kango88 I would imagine you could modify the search on line 18 to match email in your trash and it would work.

@kango88

This comment has been minimized.

Copy link

@kango88 kango88 commented Sep 11, 2019

But the action in line 20 is GmailApp.moveThreadsToTrash?

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Sep 11, 2019

@kango88 Ah good point. When I go to my Trash in Gmail, it just gives me an option to empty the trash.

Selection_082

You should just be able to lick the Empty Trash Now link and not worry about this script at all.

@kango88

This comment has been minimized.

Copy link

@kango88 kango88 commented Sep 11, 2019

unfortunately, the "empty trash now" timeout as well if there's too many emails in the trash. just like how it timeout when too many emails are selected to move to trash. so need a step 2 script to complement your step 1 script above

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Sep 11, 2019

@kango88 Got it, ya I'm not sure, you should explore the API to look for the "Delete forever" action which is what you do to a message that's in your trash which you wish to delete forever. Or you could wait the 30 days and it would happen automatically.

@kango88

This comment has been minimized.

Copy link

@kango88 kango88 commented Sep 11, 2019

Can't seem to find an action like this at https://developers.google.com/apps-script/reference/gmail/ Do you happen to know if the action of "delete forever" exist?

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Sep 11, 2019

@kingo88 I don't, sorry.

@kango88

This comment has been minimized.

Copy link

@kango88 kango88 commented Sep 11, 2019

no worries. thanks!

@Kbshopper28

This comment has been minimized.

Copy link

@Kbshopper28 Kbshopper28 commented Dec 13, 2019

I'm an artist, not a programmer. I tried following all directions, but even though my Executions say they happened, I still have the same number of emails in my account. In line 18 I changed user@example.com to my email address. I saved the script and clicked on the clock to set the trigger. myFunction, Head, Time-driven, Minutes Timer, Every Minute
Can anyone help? Thank you in advance.

@Notacoder100

This comment has been minimized.

Copy link

@Notacoder100 Notacoder100 commented Dec 14, 2019

@nealwa

This comment has been minimized.

Copy link

@nealwa nealwa commented Apr 25, 2020

This was perfect! I modified line 18 to label:unread before:yyyy/mm/dd and it worked perfectly to delete all my unread emails before 2017 utilizing the minute trigger

@Notacoder100

This comment has been minimized.

Copy link

@Notacoder100 Notacoder100 commented Apr 25, 2020

@5Isha6

This comment has been minimized.

Copy link

@5Isha6 5Isha6 commented Apr 30, 2020

Thanks for the script. @gene1wood Is there any workaround for Exception: Service invoked too many times for one day: gmail.
at batchDeleteEmail(Code:3:26)? I could only delete few thousands of mail, post which I keep getting this error

@paulbocco

This comment has been minimized.

Copy link

@paulbocco paulbocco commented May 11, 2020

This is great! @gene1wood I came across this thread looking to delete emails from a particular folder that were older than 3 months...I modified the code above to do that and I entered the date that is 3 months prior to today...to make this perfect, is there any code I can substitute in for the data YYYY/MM/DD to say "3 months older than today" so I don't have to go in and modify my project and do this each month?? p.s. the only coding experience I have is what I have learned above! Thanks alot!

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented May 11, 2020

@GTIsha , it sounds like Google is throttling you. No, I'm not aware of a workaround to them throttling you (other than possibly using a VPN to appear as another IP).

@paulbocco

is there any code I can substitute in for the data YYYY/MM/DD to say "3 months older than today" so I don't have to go in and modify my project and do this each month

I do see that in Gmail it supports dates within a window of time from now (e.g. within the last 3 months would be https://mail.google.com/mail/u/0/#advanced-search/subset=all&within=3m&sizeoperator=s_sl&sizeunit=s_smb ) however I don't see anything that lets you assert a date and define a window older than that date (only newer)

@paulbocco

This comment has been minimized.

Copy link

@paulbocco paulbocco commented May 11, 2020

@GTIsha , it sounds like Google is throttling you. No, I'm not aware of a workaround to them throttling you (other than possibly using a VPN to appear as another IP).

@paulbocco

is there any code I can substitute in for the data YYYY/MM/DD to say "3 months older than today" so I don't have to go in and modify my project and do this each month

I do see that in Gmail it supports dates within a window of time from now (e.g. within the last 3 months would be https://mail.google.com/mail/u/0/#advanced-search/subset=all&within=3m&sizeoperator=s_sl&sizeunit=s_smb ) however I don't see anything that lets you assert a date and define a window older than that date (only newer)

Thanks @gene1wood I'll have to come in periodically and change the date manually..

@Justme60

This comment has been minimized.

Copy link

@Justme60 Justme60 commented May 11, 2020

I'm sorry but I do not see where you would even put a code. I see no lines I am working on a PC. Please help if you can.

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented May 11, 2020

@Justme60 Follow the instructions on lines 8-12 on how to deploy the code

@alex9jk

This comment has been minimized.

Copy link

@alex9jk alex9jk commented May 20, 2020

Works perfect! Thank you!

@oshrishaul

This comment has been minimized.

Copy link

@oshrishaul oshrishaul commented Jun 14, 2020

Great solution!
Does anyone knows how to use the GmailApp.search to search for the following 2 conditions together:

  • all mails including all labels. should give the same results like "All Mail" label at the GUI
  • before x date
@Kbshopper28

This comment has been minimized.

Copy link

@Kbshopper28 Kbshopper28 commented Oct 1, 2020

@fancywriter

This comment has been minimized.

Copy link

@fancywriter fancywriter commented Nov 16, 2020

An attempt to run this script gave me "Gmail operation not allowed. " error from google. 😕
On line GmailApp.search(...).

@avramovic

This comment has been minimized.

Copy link

@avramovic avramovic commented Nov 27, 2020

@fancywriter Disable your popup blocker and allow the script to access your email, it worked for me and works flawlessly, except I had to put it to run every 5 minutes cause GMail was timing out when set to run every minute. Each time it's executed it runs for about 2-3 minutes and deletes up to 500 threads, so it should be done by the morning (in my case)

@timur-tabi

This comment has been minimized.

Copy link

@timur-tabi timur-tabi commented Jan 4, 2021

When I try to implement this script, I was prompted to log into my Gmail account. At that point, I got this pop-up message:

This app is blocked

This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.

How do I get around this?

Update: I wrote up instructions to get around this issue: https://docs.google.com/document/d/1PLfAnNus-B87gHS1pkbmzFTkWckAPNcqmvO7hFo_gBc/

@sharan779

This comment has been minimized.

Copy link

@sharan779 sharan779 commented Jan 26, 2021

This is great. thanks much!

@DestinyChan281

This comment has been minimized.

Copy link

@DestinyChan281 DestinyChan281 commented Feb 23, 2021

hello, im trying to figure this out
i wanna delete emails from my social inbox but im having trouble figuring out how to set up this code

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Feb 23, 2021

@DestinyChan281 Follow the instructions in the comments of the code above and share what step you're getting stuck at and what problem you're encountering.

@DestinyChan281

This comment has been minimized.

Copy link

@DestinyChan281 DestinyChan281 commented Feb 24, 2021

nevermin, I think I figured it out
though i did get this error message alot
"Exception: Service invoked too many times for one day: gmail.
batchDeleteEmail @ Code.gs:19"

@gene1wood

This comment has been minimized.

Copy link
Owner Author

@gene1wood gene1wood commented Feb 24, 2021

Service invoked too many times for one day

Sounds like Google is ratelimiting calls to the moveThreadsToTrash endpoint (or the search endpoint)

@DestinyChan281

This comment has been minimized.

Copy link

@DestinyChan281 DestinyChan281 commented Feb 24, 2021

oh okay.
I guess I’ll just keep coming back to the script each day to reset the rate limit

@newboydj169

This comment has been minimized.

Copy link

@newboydj169 newboydj169 commented Apr 6, 2021

really a nice feature, thank you so much.

@Kilarisowjanya

This comment has been minimized.

Copy link

@Kilarisowjanya Kilarisowjanya commented Apr 6, 2021

I want to delete mails from Social Label and done the steps mentioned in the comments exactly but It's not deleting my mails but the code is running successfully. I can see the triggers are happening at every 1 minute interval.
Kindly help me if anyone have any idea on it.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment