Auto-deletion or archiving of Email in Gmail for messages older than X days

If you want to keep your Gmail inbox clean and automatically delete messages older than a certain number of days, you can use a script that automatically moves the messages to the trash .
The script in Google Apps and usable for free by everyone, allows you to plan the archiving or deletion of old emails .
Thus, for example, it is possible to automatically delete emails of offers or other expired promotional emails and organize the incoming mail if you receive too many emails automatically.
READ ALSO: Find Email to be deleted in Gmail
To use this Google script you must first create a filter in Gmail on messages from sites of offers or Facebook notifications or any other mail that you no longer want to see after a certain period of time.
So, for example, all messages from @ groupon.com must be marked with the "delete me" label.
The script will not act on all messages indiscriminately but only for those to which the delete me label is applied (you can also give a different name to this label if you are able to edit the script).
As explained in the guide to create filters in Gmail they are created from the settings -> filters tab or, by opening a message, from the top button More .
You can also create a filter based on the subject or the receiving Email address if you are redirecting messages from an address other than Gmail.
Then go to the page to create Google scripts, create an empty project, paste the following script into the new file and save / run it from the Run menu.
function cleanUp () {
var delayDays = 2 // Enter # of days before messages are moved to trash
var maxDate = new Date ();
maxDate.setDate (maxDate.getDate () - delayDays);
var label = GmailApp.getUserLabelByName ("delete me");
var threads = label.getThreads ();
for (var i = 0; i <threads.length; i ++) {
if (threads [i] .getLastMessageDate () {
threads [i] .moveToTrash ();
}
}
In the second line you can edit the number 2 of delayDays which are the days before a message is deleted.
In the line that starts with var label there is the delete me label that you can modify (if you want to use a different label (in Italian) in the Gmail filter).
If you are not understanding what I am saying, you can leave everything as it is and run the script from the Run button where you can choose:
cleanup to perform automatic deletion
archiveinbox to perform automatic archiving without deleting messages.
Next, you need to set up triggers for this script, which means deciding how often it should run in a day.
From the Resources> All Triggers menu, set them to run for each day or hour as you prefer.
Run the script and let it stay in the background, working silently until it is stopped manually by the user.
Other scripts to use in Gmail explained in the past and very useful are:
- Receive an SMS when an email arrives on Gmail for free
- Statistics and personal report on the use of Gmail

Leave Your Comment

Please enter your comment!
Please enter your name here