I stopped deleting bank alerts and OTP emails by hand after writing a tiny Gmail script
Gmail can't delete mail by age, so a script does it for me
My inbox became inundated with bank alerts and one-time passcodes, a situation that required frequent manual deletion. The issue persisted as these codes became useless after a short time, yet bank alerts could linger in my inbox for months. Unfortunately, Gmail does not provide an option to automatically delete emails after a set period, nor does it offer a hidden setting to accomplish this. Consequently, I decided to write a small Apps Script to handle this task.
Initially, I attempted to use Gmail's filters, but soon realized their limitations. A filter only checks a message upon arrival and does not apply again later. Despite using the older_than search operator, the filter only dealt with messages matching the criteria at the time, leaving older ones untouched. As a Gmail user with a personal account, I did not have access to the Email and chat auto-deletion setting available to Google Workspace admins.
Nevertheless, I still sought a solution that would run continuously without requiring manual intervention.
My approach involved two components. First, a filter would tag incoming bank alerts, while a script would subsequently move these tagged messages to the Trash after a specified period. I opted to match bank alerts by sender address, as banks often alter their subject lines for different types of notifications. This method proved effective.
The script scans for threads older than three days and moves them to the Trash. Adjusting the DAYS value allows for longer retention periods, which is particularly useful for bank alerts that also serve as a record of my spending. Initially, the script's first run displayed a warning about Google's verification process for private scripts.
However, after clicking 'Advanced' and granting Gmail access, the script successfully connected to my account.
Given the script's extensive access to my Gmail, I carefully crafted it myself, ensuring I would review any script with delete capabilities before approving it. To add an extra layer of protection, I limited the script's functionality to a single label, excluding any starred threads. Before enabling the deletion feature, I ran the script in 'DRY_RUN' mode, which logs the date and subject of each matching email without deleting it. This allowed me to confirm that the filter was correctly identifying the relevant messages.
Upon enabling the deletion feature, I addressed my backlog manually. Despite the script's limitation of handling up to 100 threads per run, clearing my backlog only required a few manual operations on the first day. Over time, the script has maintained a manageable number of old alerts, usually between 10 to 15, depending on the frequency of my transactions and account changes.
The script has successfully trashed around 30 emails on its own within the first week, significantly reducing the clutter in my inbox. Even though trashed emails remain recoverable for 30 days, the peace of mind provided by this automated system outweighs the minimal risk.
In conclusion, while Gmail lacks a built-in feature for automatic email deletion, writing a custom Apps Script provided an effective solution to my inbox management problem. This script has not only saved me time but also given me a better understanding of my bank alerts and OTP emails, ultimately enhancing my overall Gmail experience.
Written by urgent.news from Android Police's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.