appscript.dev
Automation Beginner Gmail

Auto-archive threads once you've replied

Keep only conversations awaiting a response visible in the inbox by archiving threads where you spoke last.

Published Oct 28, 2025

A useful way to think about an inbox: every thread is either your move or the other person’s move. Once Awadesh replies to an email, the ball is in their court — there is nothing left to do until they come back. But that replied-to thread still sits in the inbox, adding to the noise and hiding the messages that genuinely need attention.

This script keeps the inbox down to just your move. Every half hour it scans recent inbox threads and, for any where the last message was sent by you, archives it. Nothing is lost — the thread is still in All Mail and will jump back to the inbox the moment a reply arrives.

What you’ll need

  • A Gmail account — no setup or labels required.
  • An understanding of the rule: a thread is archived only when you sent the most recent message. Threads where someone is waiting on you stay put.
  • Nothing else. The script reads and archives; it never deletes.

The script

// Only look at threads with activity in this window, to keep each run
// fast. A new reply pulls an older thread back into the inbox anyway.
const SEARCH_QUERY = 'in:inbox newer_than:7d';

/**
 * Archives every inbox thread whose most recent message was sent by the
 * account owner — i.e. threads now waiting on the other party.
 */
function archiveRepliedThreads() {
  // Who "you" are, so we can tell your messages from everyone else's.
  const me = Session.getActiveUser().getEmail();

  // 1. Pull recent inbox threads.
  const threads = GmailApp.search(SEARCH_QUERY);

  if (!threads.length) {
    Logger.log('No recent inbox threads — nothing to do.');
    return;
  }

  // 2. For each thread, check who sent the most recent message.
  let archived = 0;
  for (const thread of threads) {
    const messages = thread.getMessages();
    const last = messages[messages.length - 1];

    // 3. If you sent it last, the ball is in their court — archive it.
    if (last.getFrom().includes(me)) {
      thread.moveToArchive();
      archived++;
    }
  }
  Logger.log('Archived ' + archived + ' replied thread(s).');
}

How it works

  1. archiveRepliedThreads first reads the account owner’s email address with Session.getActiveUser().getEmail() so it can recognise your own messages.
  2. It runs a Gmail search for inbox threads with activity in the last seven days. The window keeps each run quick — an older thread that gets a new reply lands back in the inbox and inside the window anyway.
  3. If the search returns nothing, it logs a message and stops.
  4. For each thread it reads the message list and takes the last entry, which is the most recent message in the conversation.
  5. It checks whether that message’s From address contains your address. If it does, you spoke last, so moveToArchive removes the thread from the inbox. Threads where someone else spoke last are left alone.

Example run

Say the inbox holds four recent threads when the script runs:

ThreadLast message fromResult
Re: Q2 proposal — Harbour CoAwadesharchived
Invoice question — Lumen Ltdclientstays in inbox
Re: kickoff schedulingAwadesharchived
New brief from Old Millclientstays in inbox

After the run, the inbox shows only the two threads waiting on Awadesh. The two he already answered are archived — and if either client replies later, that thread reappears in the inbox automatically.

Trigger it

Run this every half hour so the inbox stays tidy through the day:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose archiveRepliedThreads, event source Time-driven, type Minutes timer, and select Every 30 minutes.
  4. Save, and approve the Gmail authorisation prompt on the first run.

Resurface when needed

Pair this with Snooze-and-resurface follow-up reminders so important archived threads come back to your attention if the other side stays silent for too long.

Watch out for

  • The match is getFrom().includes(me). If you send from an alias or a different address on the same account, those messages will not be recognised as yours and the thread will not archive. Add each alias to the check.
  • A thread you genuinely want kept in view — a reference you reply to but want to watch — will be archived too. Star it or apply a label and tighten SEARCH_QUERY with -is:starred to exempt it.
  • The seven-day window means a thread you replied to more than a week ago, with no further activity, will not be picked up. That is fine in practice, since stale threads have already left the inbox view.
  • GmailApp.search and per-thread reads count against Gmail service quotas. A large, busy inbox running every 30 minutes is well within limits, but a very high message volume could approach them.
  • Archiving is reversible but not free of side effects — the thread leaves the inbox immediately. Run it manually once first to be comfortable with what it catches.

Related