Clear Your Gmail OTPs and Bank Alerts: An Apps Script Solution

A tech writer developed a simple Apps Script for Gmail that automatically moves old bank alerts and one-time passcodes (OTPs) to the Trash folder after a few days. This offers a much-needed solution for personal Gmail users struggling with inbox clutter.

By |Published
Clear Your Gmail OTPs and Bank Alerts: An Apps Script Solution

Tired of manually deleting countless bank transaction alerts and expired one-time passcodes (OTPs) from your Gmail inbox? An Android Police tech specialist, Oluwaniyi Raji, has developed a clever Apps Script solution to automate this tedious task.

For Indian users especially, where OTPs are a daily occurrence for everything from UPI payments to online shopping, and bank alerts keep transaction records piling up, this kind of automation can be a huge time-saver. Gmail, by default, only automatically clears emails from Trash and Spam folders after 30 days, leaving everything else in your inbox until you remove it.

Why Gmail's built-in tools fall short

Many users first look to Gmail's filters for a solution. However, filters only check an email when it arrives, not continuously. This means if you read a bank alert a day or two after it lands in your inbox, a filter set to delete old messages wouldn't catch it.

The older_than search operator gets closer, letting you find emails older than a specific duration. But applying a filter with this only clears what matches at that moment, not on an ongoing basis. While Google Workspace admins have an email auto-deletion setting, it's not available for free personal Gmail accounts.

The two-part Apps Script solution

According to Android Police, the effective setup involves two key steps. First, you create a Gmail filter that tags incoming bank alerts and OTPs with a specific label, say 'auto-trash'. This acts as a marker for the script.

The second part is a short Apps Script that runs in the background. This script searches for any emails with the 'auto-trash' label that are older than a set number of days and moves them directly to your Gmail Trash folder. From there, Gmail's standard 30-day deletion policy takes over.

How the script works and its safety features

The script is designed with safety in mind. It uses variables like LABEL (for your chosen tag), DAYS (how long to keep emails), and MAX_THREADS (the maximum number of emails it handles per run, set to 100).

Crucially, it includes a DRY_RUN mode. When DRY_RUN is set to 'true', the script will log which emails it would delete without actually deleting them. This lets you confirm your filter is tagging the correct messages before you enable actual deletion. The script also specifically excludes starred threads, so anything you mark as important will remain untouched.

The script doesn't permanently delete emails either. They go to the Trash first, giving you a 30-day window to recover anything mistakenly moved. The writer, Oluwaniyi Raji, noted that he cleared about 300 old alerts in his first few manual runs, and then the script handled around 30 emails automatically in its first week.

Setting it up for Indian users: OTPs vs. bank alerts

This script is particularly useful for managing the sheer volume of digital communication in India. For OTPs, which are typically useless after a few minutes, you could set the DAYS variable to just 1 or 2. This ensures your inbox stays clear of expired codes.

For bank alerts, which often double as a record of your spending, you might want to keep them longer, perhaps a week or more. The flexibility of the script means you can create different filters and labels for different types of alerts, each with its own deletion window.

The writer mentioned expecting a feature like this from Gemini in Gmail. Until such an AI-powered solution arrives, this custom Apps Script offers a practical and reliable way to maintain a clutter-free inbox, especially for those in India dealing with a constant stream of transactional emails.

Related Stories