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
Automate Gmail cleanup with n8n and Google Gemini AI

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

Learn how to Build this Workflow with AI:

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.


Automate Gmail cleanup with n8n and Google Gemini AI

Visit through Desktop to Interact with the Workflow.

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 Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.