A Python tool to archive all emails from Gmail inbox using the Gmail API
Project description
Gmail Email Archiver
A Python tool to archive all emails from your Gmail inbox using the Gmail API. Emails are moved from the inbox to the archive (All Mail), keeping them accessible while decluttering your inbox.
Features
- Safe archiving: Emails are moved to archive, not deleted
- Batch processing: Process emails in configurable batches for efficiency
- Dry run mode: Preview what would be archived without making changes
- Progress tracking: Real-time feedback during the archiving process
- OAuth authentication: Secure Google OAuth 2.0 authentication
- Status checking: Check inbox count without making changes
Installation
Option 1: Install from PyPI (Recommended)
pip install archive-all-my-mails
Option 2: Install from source
-
Clone this repository:
git clone <repository-url> cd archive-all-my-mails
-
Install dependencies using uv (recommended) or pip:
# Using uv (recommended) uv sync # Or using pip pip install -e .
Gmail API OAuth Setup
Before using the archiver, you need to set up OAuth credentials with Google.
Step 1: Create a Google Cloud Project
- Go to the Google Cloud Console
- Click "Select a project" at the top of the page
- Click "New Project"
- Enter a project name (e.g., "gmail-archiver")
- Click "Create"
Step 2: Enable the Gmail API
- In your project, go to "APIs & Services" > "Library"
- Search for "Gmail API"
- Click on "Gmail API" from the results
- Click "Enable"
Step 3: Create OAuth 2.0 Credentials
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth client ID"
- If prompted, configure the OAuth consent screen:
- Choose "External" user type
- Fill in the required application information
- Add your email to test users
- For application type, select "Desktop application"
- Give it a name (e.g., "Gmail Archiver")
- Click "Create"
Step 4: Get Your Credentials
- After creating the OAuth client, you'll see your credentials:
- Client ID (looks like:
xxxxx.apps.googleusercontent.com) - Client Secret (a random string)
- Client ID (looks like:
- Copy these values - you'll need to provide them when running the script
- Keep these credentials secure and do not share them
Usage
Basic Commands
# Check inbox status (dry run to see what would be archived)
archive-gmail --dry-run
# Archive all emails with confirmation prompt
archive-gmail
# Archive all emails without confirmation
archive-gmail --yes
# Provide credentials as command line arguments
archive-gmail --client-id "your-client-id" --client-secret "your-secret"
# Process in smaller batches (default is 100)
archive-gmail --batch-size 50
First Run Authentication
When you first run the script:
- You'll be prompted to enter your Client ID and Client Secret
- A browser window will open automatically
- Sign in to your Google account
- Grant permission to access your Gmail
- The script will save authentication tokens locally (
token.pickle) for future use
Command Line Options
--dry-run: Preview what would be archived without making changes--client-id: Google OAuth Client ID--client-secret: Google OAuth Client Secret--batch-size: Number of emails to process in each batch (default: 100)--yes: Skip confirmation prompt
How It Works
- Authentication: Uses OAuth 2.0 to securely connect to your Gmail account
- Email Discovery: Fetches all emails from your inbox
- Batch Processing: Processes emails in configurable batches for efficiency
- Archiving: Removes the "INBOX" label from emails, moving them to archive
- Reporting: Provides feedback on success/failure counts
Safety Features
- No email deletion: Emails are archived, not deleted
- Dry run mode: Test the process without making changes
- Confirmation prompts: Asks for confirmation before archiving (unless
--yesflag is used) - Batch processing: Handles large inboxes efficiently
- Error handling: Graceful handling of API errors and rate limits
Security Notes
- Your Client ID and Client Secret are sensitive - keep them secure
- The script creates a
token.picklefile to store authentication tokens - Never share your credentials or commit them to version control
- The script only requests the minimum required Gmail permissions
Troubleshooting
Common Issues
"Invalid credentials"
- Double-check your Client ID and Client Secret
- Ensure you're using the correct credentials from Google Cloud Console
"Access denied"
- Make sure you've enabled the Gmail API in Google Cloud Console
- Verify OAuth consent screen is properly configured
- Check that your email is added as a test user
"Browser doesn't open for authentication"
- Try deleting
token.pickleand re-authenticating - Ensure you have a default browser configured
- Check firewall settings that might block the redirect
"Rate limit exceeded"
- The script handles rate limits automatically with exponential backoff
- If issues persist, try reducing the batch size with
--batch-size
Getting Help
If you encounter issues:
- Try running with
--dry-runfirst to test authentication - Check the error messages for specific guidance
- Verify your Google Cloud Console setup matches the instructions above
- Delete
token.pickleand re-authenticate if authentication seems stuck
License
This project is provided as-is for personal use. Please review and comply with Google's API Terms of Service when using this tool.
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 archive_all_my_mails-0.1.0.tar.gz.
File metadata
- Download URL: archive_all_my_mails-0.1.0.tar.gz
- Upload date:
- Size: 34.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70ead0f8832fb333bb1f5f93659dd0d571eaf8a51057209c4f8ef97e39b83837
|
|
| MD5 |
b2879cbf664d44e86533a4c0bd789bd6
|
|
| BLAKE2b-256 |
e9c36a0c621919e82688caa52c07d4f8819d8b3b91b1f151dd7b55c2783af45e
|
Provenance
The following attestation bundles were made for archive_all_my_mails-0.1.0.tar.gz:
Publisher:
publish.yml on Nat1anWasTaken/archive-all-my-mails
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
archive_all_my_mails-0.1.0.tar.gz -
Subject digest:
70ead0f8832fb333bb1f5f93659dd0d571eaf8a51057209c4f8ef97e39b83837 - Sigstore transparency entry: 316575354
- Sigstore integration time:
-
Permalink:
Nat1anWasTaken/archive-all-my-mails@a8105eb3dddc3029c823e8805b3ae2ef0b828057 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Nat1anWasTaken
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a8105eb3dddc3029c823e8805b3ae2ef0b828057 -
Trigger Event:
release
-
Statement type:
File details
Details for the file archive_all_my_mails-0.1.0-py3-none-any.whl.
File metadata
- Download URL: archive_all_my_mails-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fae3744f678c368c3d4ca859afa101abffadebbb7d7bcf6424bcfbf7a414291
|
|
| MD5 |
f9b3bcebe8d7bedd2cabfaf8880759e0
|
|
| BLAKE2b-256 |
a659401c5d44e53ffb4decf3bb8b06f80401f83217f1105e16f9a712a74318fb
|
Provenance
The following attestation bundles were made for archive_all_my_mails-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Nat1anWasTaken/archive-all-my-mails
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
archive_all_my_mails-0.1.0-py3-none-any.whl -
Subject digest:
8fae3744f678c368c3d4ca859afa101abffadebbb7d7bcf6424bcfbf7a414291 - Sigstore transparency entry: 316575372
- Sigstore integration time:
-
Permalink:
Nat1anWasTaken/archive-all-my-mails@a8105eb3dddc3029c823e8805b3ae2ef0b828057 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Nat1anWasTaken
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a8105eb3dddc3029c823e8805b3ae2ef0b828057 -
Trigger Event:
release
-
Statement type: