Skip to main content

Archive a full year of Gmail to mbox/Maildir and remove from your account

Project description

gmailarchiver

Archive all Gmail messages from a given calendar year to an mbox or Maildir file, then permanently delete (or trash) them from your account.


Features

  • Downloads every message in a year via the Gmail API (all labels, including Spam/Trash)
  • Saves to standard mbox or Maildir format — importable into Thunderbird, Apple Mail, Dovecot, etc.
  • Permanently deletes after archiving (or --trash to use recoverable Trash)
  • Idempotent-safe: archive written to disk before deletion is issued
  • Batch deletes (up to 1 000 IDs/call) to stay within Gmail API quotas
  • Exponential back-off on transient 429/5xx errors
  • Progress bars via rich
  • --dry-run to count messages without downloading

Setup

1. Google Cloud credentials

  1. Go to Google Cloud Console.
  2. Create a project → enable the Gmail API.
  3. Create an OAuth 2.0 Client ID → type Desktop app.
  4. Download credentials.json and place it at:
~/.gmailarchiver/credentials.json

Or pass --credentials <path> at runtime.

2. Install

pipx install gmailarchiver     # recommended
# or
pip install gmailarchiver

3. First run (OAuth consent)

The first invocation opens a browser tab for Google OAuth consent. After approval the token is cached at ~/.gmailarchiver/token.json.


Usage

gmailarchiver <year> [options]

Options

Flag Default Description
--output / -o PATH ./<year>.mbox Archive destination
--format / -f {mbox,maildir} mbox Archive format
--no-delete off Archive only; keep messages in Gmail
--trash off Move to Trash (30-day recovery) instead of permanent delete
--dry-run off Count messages, do nothing else
--credentials PATH ~/.gmailarchiver/credentials.json OAuth credentials
--token PATH ~/.gmailarchiver/token.json Cached OAuth token
--label LABEL_ID all mail Restrict to label (INBOX, SENT, custom ID); repeatable

Examples

# Archive 2022 to mbox, permanently delete from Gmail
gmailarchiver 2022 --output ~/mail/2022.mbox

# Archive 2021 to Maildir, move to Trash instead of hard-delete
gmailarchiver 2021 --output ~/mail/2021/ --format maildir --trash

# Dry run: count 2023 messages without touching anything
gmailarchiver 2023 --dry-run

# Archive only INBOX and SENT from 2020, keep in Gmail
gmailarchiver 2020 --output ~/mail/2020.mbox --no-delete \
  --label INBOX --label SENT

# Use a specific credentials file
gmailarchiver 2022 --credentials ~/secrets/gmail_oauth.json

Archive formats

Format Best for
mbox Single file, easy to move/compress, Apple Mail, Thunderbird
maildir Dovecot / Postfix import, per-message files, fast random access

Both formats are RFC-standard and widely supported.


Safety notes

  • Messages are written to disk before any delete call is issued.
  • --trash is the safer option if you want a 30-day grace period.
  • Permanent deletion via batchDelete is irreversible.
  • If the run is interrupted mid-way, re-run the same command — already-archived messages will be re-downloaded (mbox gets duplicates), but nothing is lost. Use --no-delete on a re-run if you only want to fill gaps, then delete manually.

Gmail API quota

The default Gmail API quota is 1 billion quota units/day. Each messages.get call costs 5 units; messages.batchDelete costs 50 units per call. Archiving 50 000 messages ≈ 253 000 units — well within limits. If you hit quota, the tool backs off and retries automatically.


Development

git clone https://github.com/youruser/gmailarchiver
cd gmailarchiver
pip install -e ".[dev]"
pytest

License

Apache 2.0

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_yr_archiver-0.1.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

gmail_yr_archiver-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file gmail_yr_archiver-0.1.1.tar.gz.

File metadata

  • Download URL: gmail_yr_archiver-0.1.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for gmail_yr_archiver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ddd9cf7c6e4bab2d897b3d9356c7dedb091a29947c4837e912a515d2cc0dbe5b
MD5 6ee29b95961e02d577736508d152882c
BLAKE2b-256 fa0b62f3646a7f58be45192ffdc42236a50140677b35db3c60873e3782c4bfd9

See more details on using hashes here.

File details

Details for the file gmail_yr_archiver-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gmail_yr_archiver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77a3a2180c44a25d44cccfc2092025c4485284b2a4efd8e837c644426478e628
MD5 2617da90fdc14ae8adceabdc0c792a73
BLAKE2b-256 7b11177e698000b0eee6e7ea92b10d8178b4b721722be5db964817ae069f7972

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