Home » Technology » How to Auto-Delete Old OTPs and Bank Alerts in Gmail Using a Simple Script

How to Auto-Delete Old OTPs and Bank Alerts in Gmail Using a Simple Script

Managing a personal inbox can feel like an endless chore, especially when repetitive clutter like bank alerts, security notifications, and one-time passcodes pile up week after week. While a verification code is completely useless after a few minutes, transaction alerts and balance updates can linger in your primary view for months, making it difficult to find important conversations. For those using a standard personal account, according to recent reports, Google does not offer a native automated setting to delete messages by age outside of the trash and spam folders.

Because standard Gmail filters only run on incoming mail and cannot purge existing messages on a recurring schedule, users are often left performing tedious manual cleanups. Fortunately, a lightweight solution exists that requires no paid subscriptions or third-party extensions. By combining a simple inbox label, a targeted filter, and a short custom script written in Google Apps Script, you can establish an automated background routine that quietly sweeps away temporary messages before they overwhelm your digital workspace.

Understanding the Limitations of Native Gmail Tools

Standard search operators like older_than can help find dated messages, but applying them through a standard filter only cleans up what matches your inbox at the exact moment the filter is created. It offers no ongoing, recurring automation for future arrivals. Meanwhile, advanced auto-deletion policies that manage message lifecycles automatically are typically restricted to enterprise environments running Google Workspace administrator consoles.

This leaves free personal account holders searching for workarounds. Bulk deletion tools can clear out thousands of messages at once, but they require constant manual intervention every few weeks. Building a custom automation solves this problem by running quietly in the cloud on a predictable schedule, ensuring your inbox remains tidy without requiring you to sort through hundreds of redundant notifications by hand.

Step-by-Step Setup: Creating Your Auto-Trash System

Setting up an automated deletion routine takes just a few minutes and relies entirely on built-in Google developer tools. The process is split into a simple tagging rule and a short script execution that handles the cleanup.

First, navigate to your inbox settings and create a new label named auto-trash. Next, build a filter that targets the specific notifications you want to clear, such as verification codes or specific sender addresses from financial institutions, and set that filter to automatically apply your new label.

Once your tagging system is in place, open script.google.com on a desktop browser to start a new project. Paste the code snippet below into the editor, making sure to leave the dry run safety feature enabled during your initial test run.

const LABEL = 'auto-trash';
const DAYS = 3;
const MAX_THREADS = 100;
const DRY_RUN = true;
function trashOldAlerts() {
  const query = 'label:' + LABEL + ' older_than:' + DAYS + 'd -is:starred';
  const threads = GmailApp.search(query, 0, MAX_THREADS);
  if (DRY_RUN) {
    threads.forEach(function (thread) {
      console.log(thread.getLastMessageDate() + ' | ' + thread.getFirstMessageSubject());
    });
    console.log(threads.length + ' threads matched');
    return;
  }
  if (threads.length > 0) {
    GmailApp.moveThreadsToTrash(threads);
  }
  console.log(threads.length + ' threads moved to Trash');
}

After pasting the code, click the run button. Because this is a custom script accessing your personal data, Google will display an unverified app warning. You can safely bypass this by clicking advanced options and opening the link to your project to grant the necessary permissions. Finally, click the clock icon in the sidebar to add a daily time-driven trigger that runs the function automatically every twenty-four hours.

Safety Guardrails and Best Practices

Allowing a script to interact with your email requires strict safety controls to prevent accidental data loss. The script above incorporates several guardrails designed to protect your important messages from being permanently deleted.

By targeting a single isolated label rather than your entire inbox, the script remains strictly quarantined to messages you have explicitly chosen to filter. Furthermore, the search query explicitly excludes any threads that you have manually starred, ensuring your important items remain untouched even if they match the date criteria.

Running the script with the dry run mode enabled first allows you to view a log of matching subjects without actually modifying your mail, giving you a chance to verify that your filter rules are working correctly. Because the script moves items to the trash rather than purging them permanently, any mistakenly deleted message remains fully recoverable in your trash folder for up to thirty days.

Key Takeaways

  • Google does not provide a native automated age-deletion setting for standard personal Gmail accounts.
  • You can automate inbox cleanups by combining a custom label, a targeted filter, and Google Apps Script.
  • Built-in safety guards like dry run modes and starred message exclusions prevent accidental data loss.
  • Deleted messages are safely stored in the trash folder for up to thirty days before permanent removal.

Join our community by subscribing to our Weekly Newsletter to stay updated on the latest AI updates and technologies, including the tips and how-to guides.

(Also, follow us on Instagram @tid_technology for more updates in your feed and our WhatsApp Channel to get daily news straight to your Messaging App).

Admin

Writes about technology, AI, and everything next at The Inner Detail.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top