A simple Python tool to analyze Gmail storage usage by label. This utility uses IMAP to calculate the storage space used by emails associated with specific labels or all labels in your Gmail account.
- 📊 Analyze storage usage for a specific Gmail label
- 📈 Get storage breakdown for all labels at once
- 🔍 List all available labels in your account
- 📏 Display results in bytes, MB, and GB
- 🔒 Simple IMAP authentication with app-specific passwords
- ⚡ Fast - uses batch operations and lightweight RFC822.SIZE
- 🎯 No external dependencies - uses Python's built-in imaplib
- Python 3.7 or higher
- Gmail account with IMAP enabled
- App-specific password (if using 2-Step Verification)
- Clone this repository:
git clone https://github.com/YOUR_USERNAME/gmailLabelStorage.git
cd gmailLabelStorage-
No dependencies to install! Uses Python's built-in libraries.
-
Set up credentials:
- Create a
.envfile in the project directory with:GMAIL_EMAIL=your.email@gmail.com GMAIL_PASSWORD=your-app-specific-password - Or set environment variables
GMAIL_EMAILandGMAIL_PASSWORD
- Create a
-
Generate an app-specific password:
- Go to Google Account App Passwords
- Select "Mail" and your device
- Copy the generated password
-
Enable IMAP in Gmail:
- Gmail Settings → Forwarding and POP/IMAP
- Enable IMAP access
python gmailLabelStorage.py --list-labelspython gmailLabelStorage.py --label "Important"python gmailLabelStorage.py============================================================
Storage Analysis for Label: Work
============================================================
Message Count: 1,234
Total Size (Bytes): 524,288,000
Total Size (MB): 500.00
Total Size (GB): 0.4883
============================================================
================================================================================
Storage Analysis for All Labels
================================================================================
Label Messages Size (MB) Size (GB)
--------------------------------------------------------------------------------
INBOX 5,432 2,500.50 2.4419
Work 1,234 500.00 0.4883
Personal 892 300.25 0.2932
...
--------------------------------------------------------------------------------
TOTAL 7,558 3,300.75 3.2234
================================================================================
On first run, the script will:
- Open your default web browser
- Ask you to sign in to your Google account
- Request permission to read your Gmail data
- Save credentials locally in
token.jsonfor future use
- The script only requests read-only access to your Gmail account
- Credentials are stored locally in
token.json - Never share your
credentials.jsonortoken.jsonfiles - Add both files to
.gitignore(already included)
The Gmail API has rate limits. For accounts with many messages, the script may take some time to complete. Progress indicators are shown during processing.
This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0).
You are free to:
- Share — copy and redistribute the material in any medium or format
- Adapt — remix, transform, and build upon the material
Under the following terms:
- Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made
- NonCommercial — You may not use the material for commercial purposes
See the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Make sure you've downloaded your OAuth credentials from Google Cloud Console and saved them as credentials.json in the project directory.
Try deleting token.json and re-authenticating.
The Gmail API has rate limits. For large mailboxes, consider analyzing specific labels instead of all labels at once.
The tool uses IMAP's RFC822.SIZE attribute to calculate email sizes:
- Connects to Gmail via IMAP
- Selects the specified label/folder
- Fetches RFC822.SIZE for all messages in batches
- Aggregates sizes and displays results
This tool provides storage based on RFC822.SIZE from IMAP. Values represent the size of the raw email message (including headers, body, and attachments). Actual storage usage in Gmail may vary slightly due to Gmail's internal storage optimizations.