Skip to main content

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.

Python License Gmail API


โœจ 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

  1. Go to Google Cloud Console
  2. Create a new project (e.g. gmail-cleanup)
  3. Go to APIs & Services โ†’ Library โ†’ search for Gmail API โ†’ Enable it
  4. 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
  5. Place credentials.json in the project folder
  6. 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.json and token.pickle are 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 trash is 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 resultSizeEstimate shown 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gmail_cleanup_agent-1.0.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gmail_cleanup_agent-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

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

Hashes for gmail_cleanup_agent-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fd1c5a01e5f57be3ba688d562e45c956cacdfc6d7a6b651c8db879408e98c1d4
MD5 1366d4b5e493be331870a6bdfeba2291
BLAKE2b-256 9c01b8a1c28d50dea66674d5fcbc19e6b00431bde67f9fb8b8796d78fabefabd

See more details on using hashes here.

File details

Details for the file gmail_cleanup_agent-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gmail_cleanup_agent-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94eaf7fa583e19e597de281e16222bfda34303eaaf7710d00ee0aeb1869156bd
MD5 ea1e3c4a7b6326510ea733510915edb2
BLAKE2b-256 c2d9e9f2c1f8bd58c06908fe7df57d2e4a1c7c85f53512294e6824d8e1755d96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page