
So, this script will delete the emails which is older than one day.Ĭlick on the Play button on the toolbar to test out the project, Google will ask for Authorization at this step. 1d represents the period of emails to keep. The line above – Newsletters and Notifications are the label names. See also Mail Service, a simpler service that only allows the sending of email. You might want to create another label to filter the notifications too) This Service lets you send email, compose drafts, manage labels, mark messages and threads, and conduct a variety of other Gmail account management tasks. var purge new AutoPurge(10, custom, array, 1, 2, 3, 4) purge.purge(2) // > custom, array purge.purge() // > 1, 2, 3, 4 purge.value purge.
#Gmail autopurge script code#
(TIP: This piece of code is design to handle multiple labels at the same time. AutoPurge.purge ( num) Purge the given number of old items from the array or remove all items without resetting the purged counter like AutoPurge.clear (). The first time you run the sample, it prompts you to authorize access: Click Review permissions. Here is the code you need to replace the sample code in the code editor. Next to Services, click Add a service add. For our example, we are using the name “ Auto Purge Gmail Labels”. From File menu you can rename the project as you wish.

You will notice almost all the newsletters are listed using this keyword (Tips!). and at the Search box type “ unsubscribe” (without the quotes). To do this, I need to create a filter in Gmail that would sort these emails in this label. Auto-purge - Automatically delete messages after they have been in the folder for a certain amount of time, from 1 day to 1 year. I am just going to sort out all the newsletter type of emails under a label called “ Newsletters“. Setup trigger to run that script for the specific Gmail account.Create a Gmail Script that would purge them after a period of time.Identify and sort emails under a specific label.
