Skip to content

sadhgurutech/mailtrim

mailtrim

Delete years of Gmail clutter in minutes. Free, open-source. Core features need no API key.

PyPI Python 3.11+ License: MIT CI


🀯 495 emails deleted Β· 87.4 MB freed in 8s using mailtrim πŸ’₯ 34% of your inbox is clutter β€” caused by just 3 senders.

mailtrim is a CLI tool that finds inbox clutter, ranks it by impact, and bulk-deletes it safely β€” with a 30-day undo window.

Core workflow (stats, purge, undo) is fully local β€” no API key required, nothing sent anywhere. Optional AI commands (triage, bulk, avoid, digest, rules --add) send only email subjects and 300-character snippets to Anthropic for classification β€” never full body content. See Anthropic's privacy policy for how API data is handled on their side.

No subscription. No black box.


Why not SaneBox / Superhuman?

The paid tools charge $7–$40/month, process your email on their servers, and still don't solve the problems that matter most:

Problem SaneBox / Superhuman mailtrim
"Remind me only if they haven't replied" βœ— Not solved βœ… Conditional follow-up
Why did AI move this email? βœ— Black box βœ… One-line explanation per email
Natural language bulk cleanup βœ— Not solved βœ… "Archive newsletters older than 60 days"
30-day undo for bulk operations βœ— Not solved βœ… Full undo log
"Emails I keep avoiding" detection βœ— Not solved βœ… AI insight per avoided email
Unsubscribe success rate 70–85% βœ… Near-100% (headless browser fallback)
Privacy β€” core commands local βœ— Cloud-processed βœ… Core: local only. AI commands: subjects/snippets to Anthropic
Cost $7–$40/month Free

Privacy

  • All data stays in ~/.mailtrim/ β€” no external servers, no telemetry, no analytics
  • OAuth token is written chmod 0o600 (owner read-only)
  • AI features send only email subjects and snippets to Anthropic β€” never full body content. See Anthropic's privacy policy for their data handling.
  • No AI key? β€” everything except triage, bulk, avoid, digest, and rules --add works without one
  • Why gmail.modify scope? This grants read, compose, trash, and label access β€” mailtrim uses it to list messages, move mail to Trash, and manage labels. The scope technically permits reading full body content; mailtrim fetches metadata only and never reads or stores body text.
  • Why gmail.send scope? The follow-up command creates reminder drafts. It is never called by stats, purge, triage, bulk, undo, or any cleanup command. If you don't use follow-up, this permission is never exercised.
  • Revoking access: Go to myaccount.google.com/permissions and remove mailtrim. Delete ~/.mailtrim/token.json locally to complete the removal.
  • See PRIVACY.md for the full data flow

What's free vs. paid?

Feature Commands Cost
Inbox analysis + bulk delete stats, purge, undo, sync, unsubscribe, follow-up, rules --run Free β€” no API key needed
AI classification + NL cleanup triage, bulk, avoid, digest, rules --add Requires Anthropic API key Β· ~$0.01–0.05 per run

The core cleanup workflow β€” scan, rank, delete, undo β€” costs nothing and requires no AI key. AI features are optional and pay-per-use; there is no subscription.


Quick start (~20 minutes first time, ~30 seconds after)

1. Install

git clone https://github.com/sadhgurutech/mailtrim
cd mailtrim
python3 -m venv venv && source venv/bin/activate
pip install -e .

# Optional: headless browser for near-100% unsubscribe success
pip install -e ".[headless]" && playwright install chromium

2. Get Gmail API credentials (one-time setup, ~15 minutes)

This is a standard OAuth setup β€” you're authorising yourself to access your own inbox. Google never charges for this. You only do this once; after that, mailtrim auth refreshes your token automatically.

Stuck? The OAuth consent screen step trips up most people. When asked for "User type", choose External. Under "Test users", add your own Gmail address. That's it β€” you don't need to publish the app.

  1. Go to console.cloud.google.com β†’ New project
  2. APIs & Services β†’ Enable APIs β†’ search Gmail API β†’ Enable
  3. OAuth consent screen β†’ External β†’ add your Gmail as a test user
  4. Credentials β†’ Create β†’ OAuth 2.0 Client ID β†’ Desktop app β†’ Download JSON
  5. Save it: mv ~/Downloads/client_secret_*.json ~/.mailtrim/credentials.json

Scopes requested: gmail.modify (read, trash, label management) and gmail.send (follow-up drafts). gmail.modify grants the capability to read body content β€” mailtrim never does, but you should know the scope allows it.

"This app isn't verified" warning: Google shows this for any OAuth app that hasn't gone through their review process. It is expected and safe to proceed β€” you are authorising your own app to access your own inbox. Click Advanced β†’ Go to mailtrim (unsafe) to continue.

3. Authenticate

mailtrim auth
# Opens browser β†’ click Allow β†’ done

4. See what's in your inbox

mailtrim stats

Sample output (illustrative β€” your numbers will vary):

Scan complete β€” 2,000 emails Β· 38 senders

34% of your inbox is clutter β€” caused by just 3 senders. 87.4 MB gone in one command.

TOTAL RECLAIMABLE SPACE
  You can safely free ~87.4 MB (34.0% of scanned inbox)
  from your top 3 senders Β· Each cleanup takes ~3-5s
  All deletions go to Trash β€” undo anytime

 #  Impact         Sender                Emails  Size     Oldest       Risk
 1  100 (High)     LinkedIn Jobs            312  44.0MB   847d ago     Safe to clean
 2   82 (High)     Substack Weekly          183  26.1MB   512d ago     Safe to clean
 3   51 (Medium)   GitHub Notifications     147   9.3MB    91d ago     Low risk
 4   29 (Low)      Shopify                   94  12.2MB   203d ago     Safe to clean
 5   18 (Low)      Medium Daily Digest       87  11.4MB   445d ago     Safe to clean

Impact = 60% storage + 40% volume (0-100)

5. Bulk delete the offenders

mailtrim purge

Sample output (illustrative):

  Top Email Offenders  (823 emails Β· 102.3 MB)
 # β”‚ Sender                      β”‚ Emails β”‚ Size  β”‚ Latest  β”‚ Sample subject
───┼─────────────────────────────┼────────┼───────┼─────────┼─────────────────────────────
 1 β”‚ LinkedIn Jobs <jobs@li...>  β”‚   312  β”‚  44MB β”‚ Apr 03  β”‚ 12 new jobs matching your...
 2 β”‚ Substack <hello@subst...>   β”‚   183  β”‚  26MB β”‚ Apr 01  β”‚ This week: AI is eating...
 3 β”‚ GitHub <noreply@github...>  β”‚   147  β”‚   9MB β”‚ Apr 04  β”‚ [myrepo] New issue opened...

Select senders to delete.
Enter numbers (1,3), ranges (1-5), all, or q to quit.

Your selection: 1,2

Selected 2 senders β€” 495 emails (70 MB):
  βœ• LinkedIn Jobs (312 emails)
  βœ• Substack (183 emails)

Move 495 emails to Trash? (undo available for 30 days) [y/N]: y

βœ“ Moved 495 emails to Trash. Undo log ID: 1 (mailtrim undo 1)

6. Share what you cleaned

mailtrim stats --share

The command outputs the following text, ready to copy and paste:

🀯 495 emails deleted · 87.4 MB freed in 8s using mailtrim
   β€’ 3 senders responsible
   β€’ Core cleanup runs locally β€” no API key needed
   β€’ My inbox was 34% clutter β€” now it's clean
   β€’ ~41 min of reading time reclaimed

Free forever. β†’ https://github.com/sadhgurutech/mailtrim

All Commands

stats β€” Quick inbox overview (no AI needed)

mailtrim stats
mailtrim stats --json   # machine-readable output

purge β€” Bulk delete by sender (no AI needed)

How the Risk/Confidence score works:

Three signals combine to estimate how safe bulk-deletion is (0–100):

Signal Weight Logic
List-Unsubscribe header present 30 pts Sender self-identifies as bulk/marketing
Age β‰₯ 180 days in inbox up to 35 pts Emails sitting >6 months are rarely actionable
Volume β‰₯ 50 from one sender up to 35 pts High frequency = almost certainly automated

🟒 β‰₯70 = Safe to clean Β· 🟑 40–69 = Low risk Β· πŸ”΄ <40 = Review first

Scores are heuristics β€” the 30-day undo exists precisely because no heuristic is perfect.

mailtrim purge                          # sort by email count (default)
mailtrim purge --sort oldest            # show oldest clutter first
mailtrim purge --sort size              # largest senders first
mailtrim purge --query "older_than:1y"  # custom query
mailtrim purge --unsub                  # also unsubscribe while deleting
mailtrim purge --permanent              # skip Trash β€” IRREVERSIBLE
mailtrim purge --json                   # output sender list as JSON

sync β€” Pull inbox into local cache

mailtrim sync             # last 200 messages
mailtrim sync --limit 500
mailtrim sync --query "in:inbox is:unread"

triage β€” AI inbox classification

mailtrim triage           # classify unread inbox
mailtrim triage --limit 50

Every email gets: priority (high/medium/low) Β· category Β· why Β· suggested action

bulk β€” Natural language bulk operations

mailtrim bulk "archive all newsletters I haven't opened in 60 days"
mailtrim bulk "delete all emails from noreply@* older than 1 year"
mailtrim bulk "label receipts from order@ or receipt@ senders"
mailtrim bulk "archive LinkedIn notifications" --dry-run  # preview first

undo β€” Reverse a bulk operation (within 30 days)

mailtrim undo        # list recent operations
mailtrim undo 42     # undo operation #42

follow-up β€” Conditional follow-up tracking

mailtrim follow-up <message-id> --days 3   # remind only if no reply in 3 days
mailtrim follow-up --list                   # see what's due today
mailtrim follow-up --sync                   # check threads for replies

avoid β€” Emails you keep putting off

mailtrim avoid                               # show with AI insight
mailtrim avoid --no-insights                 # faster, no AI
mailtrim avoid --process <id> --action archive

unsubscribe β€” Unsubscribe that actually works

mailtrim unsubscribe newsletters@company.com
mailtrim unsubscribe --from-query "label:newsletters" --limit 20
mailtrim unsubscribe --history

rules β€” Recurring automation

mailtrim rules --add "archive LinkedIn notifications older than 7 days"
mailtrim rules --list
mailtrim rules --run
mailtrim rules --run --dry-run

digest β€” Weekly inbox summary

mailtrim digest

Configuration

All settings via environment variables or ~/.mailtrim/.env:

Variable Default Description
ANTHROPIC_API_KEY (not set) Anthropic API key. Without it, mock AI mode is used.
MAILTRIM_AI_MODEL claude-sonnet-4-6 Claude model for AI features
MAILTRIM_DRY_RUN false Global dry-run (preview without executing)
MAILTRIM_UNDO_WINDOW_DAYS 30 How long undo logs are kept
MAILTRIM_AVOIDANCE_VIEW_THRESHOLD 3 Views before an email is "avoided"
MAILTRIM_FOLLOW_UP_DEFAULT_DAYS 3 Default follow-up window
MAILTRIM_DIR ~/.mailtrim Where tokens, DB, and config are stored

~/.mailtrim/.env example:

ANTHROPIC_API_KEY=sk-ant-...
MAILTRIM_DRY_RUN=false
MAILTRIM_UNDO_WINDOW_DAYS=30

Security note: Restrict permissions on this file β€” chmod 600 ~/.mailtrim/.env β€” so only your user account can read the API key.


Testing (no credentials required)

# Run all 115 tests β€” zero API calls, zero credentials needed
python -m pytest tests/ -v

# With coverage
python -m pytest tests/ --cov=mailtrim --cov-report=term-missing

All AI paths are covered by MockAIEngine β€” the full CLI can be exercised without any API key.


Contributing

See CONTRIBUTING.md. Bug reports and feature requests welcome via GitHub Issues.


Architecture

mailtrim/
β”œβ”€β”€ config.py              # Settings (env vars, ~/.mailtrim/.env)
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ gmail_client.py    # Gmail API: OAuth, CRUD, batching, retry on 429/5xx
β”‚   β”œβ”€β”€ storage.py         # Local SQLite: emails, follow-ups, rules, undo log
β”‚   β”œβ”€β”€ ai_engine.py       # Claude API: classify, NLβ†’query, digest, avoidance
β”‚   β”œβ”€β”€ mock_ai.py         # Deterministic stub β€” full testing without API key
β”‚   β”œβ”€β”€ follow_up.py       # Conditional follow-up: only surfaces if no reply
β”‚   β”œβ”€β”€ bulk_engine.py     # NL β†’ dry-run preview β†’ execute β†’ 30-day undo
β”‚   β”œβ”€β”€ avoidance.py       # "Emails you avoid" detector + per-email AI insight
β”‚   β”œβ”€β”€ unsubscribe.py     # RFC 8058 one-click + mailto + Playwright headless
β”‚   └── sender_stats.py    # Sender aggregation for stats/purge commands
└── cli/main.py            # Typer + Rich CLI β€” 11 commands

License

MIT β€” free to use, modify, and distribute.

Packages

 
 
 

Contributors