[{"data":1,"prerenderedAt":542},["ShallowReactive",2],{"\u002Fwriting\u002Fclean-up-gmail-with-claude-code":3,"\u002Fwriting\u002Fclean-up-gmail-with-claude-code-related":541},{"id":4,"title":5,"body":6,"description":518,"extension":519,"featured":520,"kind":521,"meta":522,"navigation":523,"ogImage":524,"order":363,"path":525,"publishedAt":526,"relatedWork":527,"seo":528,"stem":529,"summary":530,"takeaways":531,"technologies":536,"videoDuration":524,"videoId":524,"__hash__":540},"articles\u002Fwriting\u002Fclean-up-gmail-with-claude-code.md","How to Clean Up Gmail with Claude Code and the Gmail API (Bulk Delete, Unsubscribe, Filters)",{"type":7,"value":8,"toc":496},"minimark",[9,13,16,21,24,40,43,48,51,62,65,69,72,76,82,85,91,94,98,101,107,110,114,120,135,139,145,149,152,158,161,167,175,179,185,188,192,195,201,204,208,212,215,218,222,225,261,272,279,283,286,292,301,307,325,328,391,394,434,445,449,460,463,467,470,485,489,492],[10,11,12],"p",{},"I cleaned up a ten-year-old Gmail account in one afternoon by typing things like \"delete the marketing, keep my receipts\" into Claude Code. All Mail went from about 39,000 emails to about 27,000. I got off about 1,250 mailing lists, and my inbox is at zero. Receipts, bank statements, travel bookings, and mail from actual people are all still there.",[10,14,15],{},"The trick is a small script that talks to the Gmail API. Claude Code wrote it, and Claude Code runs it. Below are the prompts I used, how the cleanup works, and how to set up the script yourself.",[17,18,20],"h2",{"id":19},"how-the-cleanup-works","How the cleanup works",[10,22,23],{},"Every pass followed the same four steps:",[25,26,27,31,34,37],"ol",{},[28,29,30],"li",{},"Claude reads the From, Subject, and List-Unsubscribe headers of every email. For 39,000 emails that took a few minutes.",[28,32,33],{},"It groups them by sender address and counts them.",[28,35,36],{},"It sorts each sender into delete, keep, or \"your call\", based on my keep list.",[28,38,39],{},"I answer in a sentence or two, and it moves only what I approved to Trash.",[10,41,42],{},"Claude works from patterns, so it never opens emails one at a time. It looks at who sent it, how many came, what the subject lines look like, and whether there's an unsubscribe header. A sender with 300 emails all titled \"#% off + free shipping\" is an easy call.",[44,45,47],"h3",{"id":46},"start-with-a-keep-list","Start with a keep list",[10,49,50],{},"Before deleting anything, I told Claude what to protect:",[52,53,59],"pre",{"className":54,"code":56,"language":57,"meta":58},[55],"language-text","Keep everything personal and anything from my work domains. Keep receipts,\norder confirmations, transactions, account notices, points and rewards balances,\ntravel bookings, doctor and clinic portal messages, and bank and investing\nstatements. Keep job messages from Indeed. Delete marketing, newsletters,\nand social media notifications.\n","text","",[60,61,56],"code",{"__ignoreMap":58},[10,63,64],{},"Claude saved that list and checked every later cleanup against it. It caught a few close calls I would have missed. My crypto statements came from the same sender as crypto price alerts. An airline sent flight bookings from one address and sale emails from another. A phone unlock code was sitting in a pile of promos.",[17,66,68],{"id":67},"the-prompts-i-used","The prompts I used",[10,70,71],{},"These come straight from my cleanup, lightly edited. Once the script works, you can paste them into Claude Code as they are.",[44,73,75],{"id":74},"find-the-worst-offenders","Find the worst offenders",[52,77,80],{"className":78,"code":79,"language":57,"meta":58},[55],"Go through all my email and find the senders that send me a lot of stuff that's\nbasically garbage. Give me a list of senders with how many emails each one sent,\nand I'll tell you which ones to keep.\n",[60,81,79],{"__ignoreMap":58},[10,83,84],{},"Later I ran a wider pass on the All Mail folder:",[52,86,89],{"className":87,"code":88,"language":57,"meta":58},[55],"Scan the All Mail folder. Any sender that has sent me more than 10 emails from the\nsame address, show me a table with the count, their most common subject line,\nand whether we should delete them or not.\n",[60,90,88],{"__ignoreMap":58},[10,92,93],{},"On my last pass that came back as 307 senders: 78 to delete, 218 to keep, and 11 where Claude wanted my call. The 11 were things like my work site's automatic plugin-update emails and someone who forwards me chain mail.",[44,95,97],{"id":96},"delete-what-i-approved","Delete what I approved",[10,99,100],{},"My answers were short and casual, and that worked fine:",[52,102,105],{"className":103,"code":104,"language":57,"meta":58},[55],"Delete all the recommended ones. From my work site, delete only the plugin update\nand backup summary emails. The chain mail forwards, the Eventbrite order notices,\nthe GitHub notifications, and the SMS archive copies can all go too.\n",[60,106,104],{"__ignoreMap":58},[10,108,109],{},"That moved 5,502 emails to Trash. Claude held back three starred emails from a sender I'd marked for deletion and asked about them first. (I told it to delete those too.)",[44,111,113],{"id":112},"unsubscribe-in-bulk","Unsubscribe in bulk",[52,115,118],{"className":116,"code":117,"language":57,"meta":58},[55],"For the senders I'm deleting, unsubscribe me first so they stop coming.\nIf the one-click unsubscribe doesn't work, open Gmail in Chrome and do it there.\nIf that fails too, make a filter that sends them straight to Trash.\n",[60,119,117],{"__ignoreMap":58},[10,121,122,123,126,127,134],{},"Most marketing email has a ",[60,124,125],{},"List-Unsubscribe"," header, and a lot of it supports one-click unsubscribe (",[128,129,133],"a",{"href":130,"rel":131},"https:\u002F\u002Fdatatracker.ietf.org\u002Fdoc\u002Fhtml\u002Frfc8058",[132],"nofollow","RFC 8058","). Claude sends that request from the script, so it never opens the email or loads its tracking pixels. That's how I got off about 1,250 lists.",[44,136,138],{"id":137},"clean-up-social-notifications","Clean up social notifications",[52,140,143],{"className":141,"code":142,"language":57,"meta":58},[55],"For Instagram and Facebook, I get a lot of notifications I don't need in my email.\nUnsubscribe from those and delete the old ones. Keep anything about my business\nad accounts.\n",[60,144,142],{"__ignoreMap":58},[44,146,148],{"id":147},"filter-the-things-i-want-but-not-in-my-inbox","Filter the things I want, but not in my inbox",[10,150,151],{},"My domains send DMARC reports every day. I want them now and then, just not in my inbox:",[52,153,156],{"className":154,"code":155,"language":57,"meta":58},[55],"I get a lot of DMARC report emails for my domains. Are these useful?\n",[60,157,155],{"__ignoreMap":58},[10,159,160],{},"Claude explained what they are and gave me a few options. I picked \"label them and skip the inbox\":",[52,162,165],{"className":163,"code":164,"language":57,"meta":58},[55],"Option B please.\n",[60,166,164],{"__ignoreMap":58},[10,168,169,170,174],{},"It labeled the 168 old reports and archived them. Then it created a Gmail filter in Chrome so new reports skip the inbox and get the ",[171,172,173],"strong",{},"DMARC"," label, and it checked Gmail's filter settings to make sure the filter saved.",[44,176,178],{"id":177},"check-the-numbers","Check the numbers",[52,180,183],{"className":181,"code":182,"language":57,"meta":58},[55],"How many emails remain, not counting Trash? Is that the exact count?\n",[60,184,182],{"__ignoreMap":58},[10,186,187],{},"The API gives an exact count. Gmail's web interface rounds big numbers and counts conversations instead of messages.",[44,189,191],{"id":190},"find-a-forgotten-subscription","Find a forgotten subscription",[10,193,194],{},"Cleaning email turned up something useful. I asked about a charge I didn't recognize:",[52,196,199],{"className":197,"code":198,"language":57,"meta":58},[55],"I got an email from a WordPress plugin company saying I was charged about $56,\nbut their panel shows a different amount. Can you look at what's going on?\nI meant to cancel this, and I'd like a refund.\n",[60,200,198],{"__ignoreMap":58},[10,202,203],{},"Claude found the payment receipts in my email. The plugin had renewed every year since 2022 without me noticing. The \"different amount\" was US dollars on their side and Canadian dollars after conversion on mine. Claude then wrote a refund request as a Gmail draft, and I reviewed it and sent it.",[17,205,207],{"id":206},"setting-it-up","Setting it up",[44,209,211],{"id":210},"why-not-just-use-the-claude-gmail-connector","Why not just use the Claude Gmail connector?",[10,213,214],{},"The Gmail connector in Claude is fine for reading a thread or writing a draft, and I still use it for that. It only connects to one Google account at a time, though, and it handles a few messages per call. A cleanup means counting, grouping, and moving thousands of emails at once.",[10,216,217],{},"The Gmail API can do that. It lists every message ID in the mailbox, reads just the headers in batches, and moves up to 1,000 messages to Trash in one call. Everything runs on my own computer, with no third-party service in the middle, and the login token stays on my machine.",[44,219,221],{"id":220},"_1-create-a-google-cloud-oauth-client","1. Create a Google Cloud OAuth client",[10,223,224],{},"Claude walked me through this in the browser. By hand it takes about five minutes:",[25,226,227,236,243,250],{},[28,228,229,230,235],{},"In the ",[128,231,234],{"href":232,"rel":233},"https:\u002F\u002Fconsole.cloud.google.com",[132],"Google Cloud console",", create a project (any name).",[28,237,238,239,242],{},"Enable the ",[171,240,241],{},"Gmail API"," for that project.",[28,244,245,246,249],{},"Set up the OAuth consent screen as ",[171,247,248],{},"External",", and add your own Gmail address as a test user.",[28,251,252,253,256,257,260],{},"Create an ",[171,254,255],{},"OAuth client ID"," of type ",[171,258,259],{},"Desktop app",", and download the JSON file.",[10,262,263,264,267,268,271],{},"Save the file as ",[60,265,266],{},"~\u002F.config\u002Fgmail-agent\u002Fclient_secret.json",", and run ",[60,269,270],{},"chmod 600"," on it so only you can read it.",[10,273,274,275,278],{},"The script asks for one permission, ",[60,276,277],{},"gmail.modify",". That lets it read, label, archive, and trash email. It can't permanently delete anything, which I like as a safety net.",[44,280,282],{"id":281},"_2-have-claude-build-the-script","2. Have Claude build the script",[10,284,285],{},"This is roughly the prompt to start with:",[52,287,290],{"className":288,"code":289,"language":57,"meta":58},[55],"Build me a small command-line tool in Python (run with uv) that uses the Gmail API\nto manage my Gmail. Use the OAuth Desktop client in ~\u002F.config\u002Fgmail-agent\u002F\nclient_secret.json and only the gmail.modify scope. I need commands to count,\nsearch, list top senders, trash, untrash, archive, and label emails using normal\nGmail search queries. Make every command that changes mail a dry run unless I\npass --yes. Save the login token so I only sign in once, and keep the token file\nprivate.\n",[60,291,289],{"__ignoreMap":58},[10,293,294,295,300],{},"Mine ended up as a single Python file, run with ",[128,296,299],{"href":297,"rel":298},"https:\u002F\u002Fdocs.astral.sh\u002Fuv\u002F",[132],"uv"," so there's nothing to install by hand. These are its commands:",[52,302,305],{"className":303,"code":304,"language":57,"meta":58},[55],"gmail.py auth                        sign in once (opens a browser)\ngmail.py profile                     total messages and threads\ngmail.py count  QUERY                exact number of matches for a Gmail search\ngmail.py search QUERY [-n N]         list matches (date | from | subject)\ngmail.py senders QUERY [-n N]        top senders for a search, with counts\ngmail.py trash  QUERY [--yes]        move matches to Trash\ngmail.py untrash QUERY [--yes]       restore matches from Trash\ngmail.py archive QUERY [--yes]       remove matches from the Inbox\ngmail.py label  QUERY LABEL [--yes]  add a label (created if missing)\n",[60,306,304],{"__ignoreMap":58},[10,308,309,312,313,316,317,320,321,324],{},[60,310,311],{},"QUERY"," is a normal Gmail search, like ",[60,314,315],{},"from:store.com older_than:1y"," or ",[60,318,319],{},"category:promotions",". A command that changes mail only shows how many emails match. Nothing moves until you add ",[60,322,323],{},"--yes",".",[10,326,327],{},"Most of the work happens in two functions. This one pages through every message ID:",[52,329,333],{"className":330,"code":331,"language":332,"meta":58,"style":58},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","def ids(svc, q):\n    out, token = [], None\n    while True:\n        r = svc.users().messages().list(userId=\"me\", q=q, maxResults=500,\n                                        pageToken=token).execute()\n        out += [m[\"id\"] for m in r.get(\"messages\", [])]\n        token = r.get(\"nextPageToken\")\n        if not token:\n            return out\n","python",[60,334,335,343,349,355,361,367,373,379,385],{"__ignoreMap":58},[336,337,340],"span",{"class":338,"line":339},"line",1,[336,341,342],{},"def ids(svc, q):\n",[336,344,346],{"class":338,"line":345},2,[336,347,348],{},"    out, token = [], None\n",[336,350,352],{"class":338,"line":351},3,[336,353,354],{},"    while True:\n",[336,356,358],{"class":338,"line":357},4,[336,359,360],{},"        r = svc.users().messages().list(userId=\"me\", q=q, maxResults=500,\n",[336,362,364],{"class":338,"line":363},5,[336,365,366],{},"                                        pageToken=token).execute()\n",[336,368,370],{"class":338,"line":369},6,[336,371,372],{},"        out += [m[\"id\"] for m in r.get(\"messages\", [])]\n",[336,374,376],{"class":338,"line":375},7,[336,377,378],{},"        token = r.get(\"nextPageToken\")\n",[336,380,382],{"class":338,"line":381},8,[336,383,384],{},"        if not token:\n",[336,386,388],{"class":338,"line":387},9,[336,389,390],{},"            return out\n",[10,392,393],{},"And this one changes up to 1,000 at a time:",[52,395,397],{"className":330,"code":396,"language":332,"meta":58,"style":58},"def modify(svc, mids, add=(), remove=()):\n    for i in range(0, len(mids), 1000):\n        svc.users().messages().batchModify(userId=\"me\", body={\n            \"ids\": mids[i:i + 1000],\n            \"addLabelIds\": list(add),\n            \"removeLabelIds\": list(remove),\n        }).execute()\n",[60,398,399,404,409,414,419,424,429],{"__ignoreMap":58},[336,400,401],{"class":338,"line":339},[336,402,403],{},"def modify(svc, mids, add=(), remove=()):\n",[336,405,406],{"class":338,"line":345},[336,407,408],{},"    for i in range(0, len(mids), 1000):\n",[336,410,411],{"class":338,"line":351},[336,412,413],{},"        svc.users().messages().batchModify(userId=\"me\", body={\n",[336,415,416],{"class":338,"line":357},[336,417,418],{},"            \"ids\": mids[i:i + 1000],\n",[336,420,421],{"class":338,"line":363},[336,422,423],{},"            \"addLabelIds\": list(add),\n",[336,425,426],{"class":338,"line":369},[336,427,428],{},"            \"removeLabelIds\": list(remove),\n",[336,430,431],{"class":338,"line":375},[336,432,433],{},"        }).execute()\n",[10,435,436,437,440,441,444],{},"Trashing an email adds the ",[60,438,439],{},"TRASH"," label, and archiving removes the ",[60,442,443],{},"INBOX"," label. The other commands are built on those two functions.",[44,446,448],{"id":447},"_3-sign-in-once","3. Sign in once",[10,450,451,452,455,456,459],{},"The first run opens a Google sign-in page. The app is your own and unverified, so Google warns that it \"hasn't verified this app\". Click ",[171,453,454],{},"Advanced",", then ",[171,457,458],{},"Go to (your app name)",". You only approve it once. The script saves a refresh token, so it doesn't ask again.",[10,461,462],{},"I hit one snag here. Google's account page pings the local redirect address during sign-in, and the standard sign-in helper failed with a \"state mismatch\" error. Claude replaced it with a small local listener that ignores any request without the right code. If you build your own, watch for that.",[17,464,466],{"id":465},"what-went-wrong","What went wrong",[10,468,469],{},"A few things bit me, so you can skip them:",[471,472,473,476,479,482],"ul",{},[28,474,475],{},"Gmail's web \"select all\" isn't reliable for big jobs. Early on, Claude used Gmail in Chrome to select every matching email and delete it. On long searches it reported success but only deleted part of the list, and about 14,000 emails had to be trashed again through the API. Use the API for anything over a few hundred.",[28,477,478],{},"Look over the unsubscribe list before it runs. One batch unsubscribed me from job invite emails I wanted, because they came from the same company as the job ads. Have Claude show you the list first and skim it.",[28,480,481],{},"Trash instead of deleting. Gmail keeps trashed email for 30 days, which gave me time to restore anything that turned out to matter. My script can't permanently delete, so I couldn't get this wrong.",[28,483,484],{},"Check filters and labels before deleting a sender. One sender I almost deleted fed a label I use for a house purchase. On later passes, Claude skipped starred emails and emails with my important labels unless I said otherwise.",[17,486,488],{"id":487},"keeping-it-clean","Keeping it clean",[10,490,491],{},"When Trash empties after 30 days, that space comes off my Google storage quota, so I can put off paying for a bigger Google One plan. To stay on top of it, I'll run one prompt a month: \"scan for new senders over 10 emails and show me the table.\" It takes a few minutes.",[493,494,495],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":58,"searchDepth":351,"depth":351,"links":497},[498,501,510,516,517],{"id":19,"depth":345,"text":20,"children":499},[500],{"id":46,"depth":351,"text":47},{"id":67,"depth":345,"text":68,"children":502},[503,504,505,506,507,508,509],{"id":74,"depth":351,"text":75},{"id":96,"depth":351,"text":97},{"id":112,"depth":351,"text":113},{"id":137,"depth":351,"text":138},{"id":147,"depth":351,"text":148},{"id":177,"depth":351,"text":178},{"id":190,"depth":351,"text":191},{"id":206,"depth":345,"text":207,"children":511},[512,513,514,515],{"id":210,"depth":351,"text":211},{"id":220,"depth":351,"text":221},{"id":281,"depth":351,"text":282},{"id":447,"depth":351,"text":448},{"id":465,"depth":345,"text":466},{"id":487,"depth":345,"text":488},"A step-by-step guide to cleaning up an old Gmail account with Claude Code and the Gmail API. Bulk delete by sender, unsubscribe in one click, create filters, and clean the All Mail folder, with the exact prompts I used.","md",false,"article",{},true,null,"\u002Fwriting\u002Fclean-up-gmail-with-claude-code","2026-11-26",[],{"title":5,"description":518},"writing\u002Fclean-up-gmail-with-claude-code","I pointed Claude Code at my Gmail through a small API script and cleaned out 12,000 old emails, 1,250 mailing lists, and a pile of notifications just by typing what I wanted.",[532,533,534,535],"The Gmail API can scan and change tens of thousands of emails in minutes, where Gmail's web \"select all\" quietly fails on big searches","Give Claude a keep list first (receipts, accounts, travel, work), then let it sort senders into delete, keep, and ask-me","Make every bulk command a dry run by default, and trash instead of deleting, so nothing is lost by mistake","One-click unsubscribe headers let Claude leave hundreds of mailing lists without opening a single email",[537,241,538,539,299],"Claude Code","Google Cloud","Python","ngwFop8y38MPR5kmBjWRGjNcwBEyf1KK2T-EnqjAfb0",[],1795711313029]