Gmail Archivist
A Python tool to automatically archive old Gmail messages while preserving starred conversations.
Features
- Archive Gmail messages older than a specified time threshold (e.g., 3 days, 1 week, 2 months)
- Automatically skip starred messages and all messages in their threads
- Batch processing for large operations with progress logging
- Interactive confirmation before archiving
- Configurable search iterations and batch sizes
Prerequisites
- Python 3.10 or higher
- A Google account with Gmail
- Google Cloud project with Gmail API enabled
Installation
Option 1: Install from PyPI (recommended)
pip install gmail-archivist
Option 2: Install from source
git clone https://github.com/andrewguo5/gmail-archivist.git
cd gmail-archivist
pip install -r requirements.txt
Google API Setup
Before using Gmail Archivist, you need to set up Gmail API credentials:
1. Create a Google Cloud Project
- Go to the Google Cloud Console
- Click "Select a project" → "New Project"
- Name your project (e.g., "Gmail Archivist") and click "Create"
2. Enable Gmail API
- In your project, go to APIs & Services → Library
- Search for "Gmail API"
- Click on it and press "Enable"
3. Configure OAuth Consent Screen
- Go to APIs & Services → OAuth consent screen
- Select "External" user type and click "Create"
- Fill in the required fields:
- App name: "Gmail Archivist" (or your preferred name)
- User support email: your email
- Developer contact: your email
- Click "Save and Continue"
- On the "Scopes" page, click "Save and Continue" (we'll add scopes via code)
- On the "Test users" page, add your Gmail address as a test user
- Click "Save and Continue"
4. Create OAuth Credentials
- Go to APIs & Services → Credentials
- Click "Create Credentials" → "OAuth client ID"
- Select "Desktop app" as the application type
- Name it "Gmail Archivist Client"
- Click "Create"
- Click "Download JSON" on the credential you just created
- Rename the downloaded file to
credentials.json - Place
credentials.jsonin the directory where you'll run the tool
Usage
Basic Usage
Archive emails older than 3 days (default):
python archive_messages.py
Or if installed via PyPI:
gmail-archivist
Custom Time Threshold
Archive emails older than 1 week:
python archive_messages.py --ttl 1w
Archive emails older than 2 months:
python archive_messages.py --ttl 2m
Supported time formats:
dfor days (e.g.,3d,7d)wfor weeks (e.g.,1w,2w)mfor months (e.g.,1m,6m)
Advanced Options
python archive_messages.py --ttl 7d --max-iter 500 --max-results 100
Options:
--ttl: Time threshold for archiving (default:3d)--max-iter: Maximum number of search iterations (default:1000)--max-results: Maximum emails to fetch per iteration (default:500)
First Run
On the first run:
- A browser window will open asking you to authorize the application
- Sign in with your Google account
- Click "Continue" when you see the "unverified app" warning (this is normal for personal projects)
- Grant the requested permissions
- The tool will create a
token.jsonfile to store your credentials for future runs
How It Works
- Discovery: Searches for all messages in your inbox older than the specified TTL
- Starred Protection: Identifies all starred messages and expands to include all messages in their threads
- Filtering: Removes starred messages (and their threads) from the archive list
- Confirmation: Shows a summary and asks for confirmation
- Archiving:
- For < 100 messages: Processes individually
- For ≥ 100 messages: Processes in batches of up to 1000 messages (Google API limit)
- Validation: Samples random messages to verify archiving succeeded
Batch Processing
When archiving 100+ messages, the tool automatically uses batch operations:
=== Archive Summary ===
Total messages found: 2500
Starred messages (skipped): 150
Messages to archive: 2350
=======================
Operation will be batched:
Batch size: 1000 messages per batch
Total batches: 3
Processing batch 1/3 (1000 messages)...
Batch 1/3 completed.
Processing batch 2/3 (1000 messages)...
Batch 2/3 completed.
Processing batch 3/3 (350 messages)...
Batch 3/3 completed.
Security & Privacy
- Your credentials never leave your machine
- The
credentials.jsonandtoken.jsonfiles contain sensitive data and are excluded from version control - The tool only requests
gmail.modifyscope (read and modify labels, not delete messages) - All operations are performed locally through Google's official API
Troubleshooting
"The file token.json stores invalid credentials"
Delete token.json and run the tool again to re-authenticate.
"Access blocked: Gmail Archivist has not completed the Google verification process"
Make sure you added your email as a test user in the OAuth consent screen settings.
Rate limit errors
The tool respects Google's API quotas. If you hit rate limits, wait a few minutes and try again with smaller --max-results values.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - feel free to use this tool for personal or commercial projects.
Acknowledgments
Built using the Gmail API Python Client.
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_archivist-0.1.0.tar.gz.
File metadata
- Download URL: gmail_archivist-0.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab6b4ea82db6061f5b701339cd86bae5fbcfc7bfcb1264daeecf99b3bc4e929a
|
|
| MD5 |
4760f273193f1b79f9301cfa6f037606
|
|
| BLAKE2b-256 |
6c78864817bc4756829759a24d778fb5c310fe4262525f521b3926444e108aea
|
File details
Details for the file gmail_archivist-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gmail_archivist-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a55517b7788795b8a25e56b4b80193d061c16c02d188f580d9781f6969c3933
|
|
| MD5 |
5df93aa6ea9f1a1dba3c88d49e45910d
|
|
| BLAKE2b-256 |
bf89d6c415cc466cb470dc7f108c1aa3053807cbc5aeff1ed33ceefd0f5067fb
|