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
archiveRepliedThreadsfirst reads the account owner’s email address withSession.getActiveUser().getEmail()so it can recognise your own messages.- 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.
- If the search returns nothing, it logs a message and stops.
- For each thread it reads the message list and takes the last entry, which is the most recent message in the conversation.
- It checks whether that message’s
Fromaddress contains your address. If it does, you spoke last, somoveToArchiveremoves 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:
| Thread | Last message from | Result |
|---|---|---|
| Re: Q2 proposal — Harbour Co | Awadesh | archived |
| Invoice question — Lumen Ltd | client | stays in inbox |
| Re: kickoff scheduling | Awadesh | archived |
| New brief from Old Mill | client | stays 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:
- In the Apps Script editor, open Triggers (the clock icon).
- Click Add Trigger.
- Choose
archiveRepliedThreads, event source Time-driven, type Minutes timer, and select Every 30 minutes. - 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_QUERYwith-is:starredto 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.searchand 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
Convert long email threads into a summary note
Collapse a thread's history into a Doc for handover — perfect for client transitions or vacation cover.
Updated Jun 6, 2026
Pull event RSVPs from emails into a Sheet
Parse yes/no replies to event invites and tally attendance automatically.
Updated Jun 2, 2026
Turn forwarded emails into project tasks
Forward to [email protected] and a row lands in the Projects sheet under the right client.
Updated May 30, 2026
Turn starred emails into a task list
Sync every starred thread into the Northwind Tasks sheet automatically.
Updated May 26, 2026
Alert when a label hits a backlog threshold
Warn the Northwind team in Slack when a Gmail label has more than N unread threads.
Updated Mar 31, 2026