A smart agentic CLI tool to bulk delete Gmail emails using search queries
Project description
๐ค Gmail Cleanup Agent
A smart, agentic CLI tool that automatically deletes emails matching any Gmail search query โ looping until your inbox is spotless.
Built with Python + Gmail API. Runs locally. Your data never leaves your machine.
โจ Features
- ๐ Any Gmail query โ use the full power of Gmail search syntax
- ๐ Dry-run mode โ preview matching emails before deleting anything
- ๐ Agentic loop โ keeps running until zero emails match the query
- ๐๏ธ Trash or delete โ move to Trash (recoverable) or permanently delete
- ๐จ Colorful terminal output โ clear progress reporting
- โก Interactive mode โ guided experience for non-technical users
- ๐ฅ๏ธ CLI mode โ scriptable for power users and automation
๐ Prerequisites
- Python 3.8+
- A Google account
- A Google Cloud project with Gmail API enabled (free โ setup takes ~5 minutes)
๐ Quick Start
Step 1 โ Clone the repo
git clone https://github.com/YOUR_USERNAME/gmail-cleanup-agent.git
cd gmail-cleanup-agent
Step 2 โ Set up a virtual environment
python3 -m venv venv
source venv/bin/activate # Mac/Linux
# venv\Scripts\activate # Windows
Step 3 โ Install dependencies
pip install -r requirements.txt
Step 4 โ Set up Gmail API credentials
- Go to Google Cloud Console
- Create a new project (e.g.
gmail-cleanup) - Go to APIs & Services โ Library โ search for Gmail API โ Enable it
- Go to APIs & Services โ Credentials โ Create Credentials โ OAuth 2.0 Client ID
- Application type: Desktop App
- Download the JSON file โ rename it to
credentials.json
- Place
credentials.jsonin the project folder - Go to Google Auth Platform โ Audience โ Test Users โ add your Gmail address
Step 5 โ Run it!
python3 gmail_cleanup_agent_v2.py
A browser window will open for authentication. After that, you're good to go!
๐ป Usage
Interactive Mode (recommended for first-time users)
Just run without any arguments โ it guides you through everything:
python3 gmail_cleanup_agent_v2.py
CLI Mode (for power users)
# Preview matching emails without deleting (dry run)
python3 gmail_cleanup_agent_v2.py --query "category:promotions" --dry-run
# Move to Trash (recoverable for 30 days) โ DEFAULT
python3 gmail_cleanup_agent_v2.py --query "category:promotions" --mode trash
# Permanently delete โ use with caution!
python3 gmail_cleanup_agent_v2.py --query "category:promotions" --mode delete
Arguments
| Argument | Short | Description |
|---|---|---|
--query |
-q |
Gmail search query |
--mode |
-m |
trash (default) or delete |
--dry-run |
-d |
Preview only, no deletions |
๐ Useful Query Examples
| Goal | Query |
|---|---|
| All promotional emails | category:promotions |
| All social notifications | category:social |
| All newsletter/updates | category:updates |
| Unread older than 1 year | is:unread older_than:1y |
| Everything from LinkedIn | from:linkedin.com |
| Multiple senders | from:linkedin.com OR from:facebook.com |
| Unread promotions only | is:unread category:promotions |
| Old unread emails | is:unread older_than:6m |
| All of the above at once | category:promotions OR category:social OR category:updates |
๐ Privacy & Security
- Your credentials never leave your machine โ the script runs entirely locally
credentials.jsonandtoken.pickleare listed in.gitignoreโ never commit them- The app requests
https://mail.google.com/scope โ required for delete operations - You can revoke access anytime at myaccount.google.com/permissions
โ ๏ธ Important Notes
- Always do a dry run first before deleting โ especially with broad queries
--mode trashis the safe default โ emails stay in Trash for 30 days before permanent deletion- Gmail API has rate limits โ the agent automatically pauses between rounds to avoid hitting them
- The
resultSizeEstimateshown in dry-run is capped at ~201 by the Gmail API โ the real count may be much higher. The agent will keep looping until all matching emails are gone.
๐ ๏ธ How It Works
This is a simple agentic loop:
1. Run Gmail search query
2. If emails found โ delete them in batches of 100
3. Wait briefly (rate limit protection)
4. Repeat from step 1
5. If no emails found โ done โ
It's a great example of an AI agent pattern: a loop that takes an action, observes the result, and repeats until a goal condition is met.
๐ Project Structure
gmail-cleanup-agent/
โโโ gmail_cleanup_agent_v2.py # Main agent script
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ .gitignore # Excludes credentials from git
โโโ credentials.json # โ YOU provide this (not in repo)
โโโ token.pickle # โ Auto-generated on first run (not in repo)
๐ค Contributing
Contributions welcome! Some ideas for future improvements:
- Web interface (Flask/Next.js)
- Chrome Extension
- Unsubscribe automation
- Scheduled/recurring cleanup (cron)
- Label-based archiving instead of deletion
- Support for multiple Gmail accounts
Feel free to open an issue or submit a PR!
๐ License
MIT License โ free to use, modify, and distribute.
๐ Acknowledgements
Built with Google Gmail API and Python.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gmail_cleanup_agent-1.0.0.tar.gz.
File metadata
- Download URL: gmail_cleanup_agent-1.0.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd1c5a01e5f57be3ba688d562e45c956cacdfc6d7a6b651c8db879408e98c1d4
|
|
| MD5 |
1366d4b5e493be331870a6bdfeba2291
|
|
| BLAKE2b-256 |
9c01b8a1c28d50dea66674d5fcbc19e6b00431bde67f9fb8b8796d78fabefabd
|
File details
Details for the file gmail_cleanup_agent-1.0.0-py3-none-any.whl.
File metadata
- Download URL: gmail_cleanup_agent-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94eaf7fa583e19e597de281e16222bfda34303eaaf7710d00ee0aeb1869156bd
|
|
| MD5 |
ea1e3c4a7b6326510ea733510915edb2
|
|
| BLAKE2b-256 |
c2d9e9f2c1f8bd58c06908fe7df57d2e4a1c7c85f53512294e6824d8e1755d96
|