What This Automation Does
This workflow cleans up Gmail inboxes automatically. It scans emails batch by batch. It uses Google Gemini AI to check if emails are unwanted, marketing, or spam. Emails that pass a threshold of 0.5 confidence get deleted. Notifications go out on Telegram about deleted, skipped, or failed emails.
This saves hours of manual sorting. It keeps the inbox tidy and alerts you about email handling.
How This Workflow Works (Input → Process → Output)
Inputs
- Gmail inbox emails fetched in 14-day chunks.
- Google Gemini AI model receives full email data for classification.
- Telegram Bot to send notifications.
Processing Steps
- Fetch emails from Gmail using filters to avoid emails labeled “n8n-skipped.
- Increment paging variable to process emails batch by batch.
- Send email data to Google Gemini Chat Model for classification with confidence scores.
- Parse AI output into structured fields like emailId and confidence values.
- Check if any confidence score is above 0.5 to decide deletion.
- Delete unwanted emails in Gmail when threshold exceeded.
- Send Telegram notifications for deleted and non-deleted emails.
- Catch AI failures and notify via Telegram.
- Loop through all email batches until inbox fully processed.
Outputs
- Emails deleted automatically from Gmail.
- Clear Telegram messages report deleted, skipped, or errors.
- Logs of email processing to track progress.
Who Should Use This Workflow
This is good for people with busy Gmail accounts flooded by promos and spam. If manual sorting wastes too much time, this helps. It fits people who want smart AI to sort emails based on content, not just basic filters.
This suits users who want clear notifications on which emails are deleted or skipped, keeping them informed.
Tools and Services Used
- Gmail API: Fetches and deletes emails with OAuth2 access.
- Google Gemini (PaLM) API: Classifies email content with AI confidence scores.
- Telegram Bot API: Sends notifications about email actions.
- n8n Automation Platform: Orchestrates workflow nodes and logic.
Beginner Step-by-Step: How to Use This Workflow in n8n for Production
Step 1: Download Workflow File
- Find the Download button on this page. Click it to get the workflow file (.json).
Step 2: Import Workflow into n8n
- Open n8n editor.
- Use the menu option “Import from File”.
- Select the downloaded workflow file.
Step 3: Configure Credentials and IDs
- Add Gmail OAuth2 credentials with permissions including email deletion.
- Add Google Gemini API Key for AI classification.
- Set Telegram Bot Token and Chat ID for notifications.
- Check and update any IDs, emails, or labels if your setup differs.
- Review the AI prompt inside the Google Gemini Chat Model node to customize if needed.
Step 4: Test Workflow Manually
- Use the Manual Trigger node to start the workflow.
- Check Telegram messages to make sure notifications send properly.
- Verify emails are classified and deleted (or skipped) as expected.
Step 5: Activate Workflow for Production
- Change the trigger from manual to a scheduled trigger or webhook to run automatically.
- Monitor Telegram notifications regularly for tracking.
- Adjust thresholds or settings if needed based on email behavior.
Using this simple import and basic config method gets this intelligent email cleanup running quickly.
Customization Ideas
- Change Gmail query dates to scan shorter or longer batches, such as 7 or 30 days.
- Adjust confidence cutoff in the If node for more or less aggressive deletions.
- Enhance AI prompt inside Google Gemini Chat Model to classify other email types or add custom rules.
- Modify Telegram message format or send to different chat IDs for team alerts.
Troubleshooting and Edge Cases
- Can’t Delete Email Errors: Usually means permissions missing or wrong emailId used.
- AI Classification Fails: May be API quota or prompt formatting issues.
- Workflow Loops Without Progress: Paging variables may not increment properly.
- Emails protected by Gmail policies can’t be deleted even if classified.
Pre-Production Checks
- Confirm Gmail OAuth2 has full access including delete.
- Test Telegram Bot messages are received correctly.
- Validate Google Gemini API Key and model access.
- Run workflow manually to verify correct classification and deletion.
- Ensure AI output parser schema matches the AI response structure.
Deployment Instructions
After testing, set the workflow to run automatically via a Schedule Trigger or webhook.
Keep checking Telegram notifications for errors or cleanup summaries.
Adjust confidence thresholds if needed to reduce false deletes.
Protect API keys and renew before expiration.
For hosting n8n on your own server, use self-host n8n resources.
Summary of Benefits and Results
✓ Automatically sorts and deletes unwanted Gmail emails based on content.
✓ Saves hours weekly that manual sorting used to take.
✓ Keeps inbox clean from marketing and spam emails.
✓ Sends Telegram notifications for clear tracking of email actions.
✓ Handles large inboxes in manageable 14-day batches.
