Automate Gmail Cleanup Using n8n and Google Gemini AI

This n8n workflow uses Google Gemini AI to intelligently classify and delete unwanted Gmail emails, saving you hours of manual email sorting. Automate email cleanup with precise AI-driven filtering and real-time Telegram notifications.
gmail
lmChatGoogleGemini
telegram
+7
Workflow Identifier: 1089
NODES in Use: gmail, manualTrigger, lmChatGoogleGemini, agent, outputParserStructured, if, telegram, set, aggregate, noOp

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

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

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

  1. Find the Download button on this page. Click it to get the workflow file (.json).

Step 2: Import Workflow into n8n

  1. Open n8n editor.
  2. Use the menu option “Import from File”.
  3. Select the downloaded workflow file.

Step 3: Configure Credentials and IDs

  1. Add Gmail OAuth2 credentials with permissions including email deletion.
  2. Add Google Gemini API Key for AI classification.
  3. Set Telegram Bot Token and Chat ID for notifications.
  4. Check and update any IDs, emails, or labels if your setup differs.
  5. Review the AI prompt inside the Google Gemini Chat Model node to customize if needed.

Step 4: Test Workflow Manually

  1. Use the Manual Trigger node to start the workflow.
  2. Check Telegram messages to make sure notifications send properly.
  3. Verify emails are classified and deleted (or skipped) as expected.

Step 5: Activate Workflow for Production

  1. Change the trigger from manual to a scheduled trigger or webhook to run automatically.
  2. Monitor Telegram notifications regularly for tracking.
  3. 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.


Frequently Asked Questions

Yes, other AI nodes like OpenAI GPT can be used but prompt and output parsing must be updated accordingly.
Yes, deletion actions count toward Gmail API quota and should be monitored.
Email content is sent to Google Gemini API, so verify data privacy policies allow this or anonymize sensitive data.
Yes, the paging system batches emails by date to avoid timeouts and handle large volumes efficiently.

Promoted by BULDRR AI

Related Workflows

Automate Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation Workflows in n8n

A complete beginner guide to building an AI SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free