---
title: How I Used Python to Automate Cleaning 160k Unread Emails
description: Inbox out of control? Here's a Python script to bulk delete thousands of promotional or junk emails in just 10 minutes.
image: https://blog.continuus.ai/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019.png
---

[Skip to content](https://blog.continuus.ai/python-automate-cleaning-unread-emails#main-content)

[![Continuus Logo 2024 - Horizontal - Full Color - Padded-1](https://blog.continuus.ai/hs-fs/hubfs/Continuus%20Logo%202024%20-%20Horizontal%20-%20Full%20Color%20-%20Padded-1.png?width=1803&height=665&name=Continuus%20Logo%202024%20-%20Horizontal%20-%20Full%20Color%20-%20Padded-1.png)](https://www.continuus.ai/)

- [Services](https://www.continuus.ai/services)
- [Partners](https://www.continuus-technologies.com/partners)
- Company
  
  Show submenu for Company 
  
    - [About](https://www.continuus.ai/about)
    - [Our Team](https://www.continuus.ai/our-team)
    - [Careers](https://www.continuus.ai/careers)
    - [News](https://www.continuus.ai/news-recognition)
- [Resources](https://blog.continuus-technologies.com/)
- [Contact](https://www.continuus-technologies.com/contact)

Open main navigation

Close main navigation

- [Services](https://www.continuus.ai/services)
- [Partners](https://www.continuus-technologies.com/partners)
- Company
  
  Show submenu for Company 
  
    - [About](https://www.continuus.ai/about)
    - [Our Team](https://www.continuus.ai/our-team)
    - [Careers](https://www.continuus.ai/careers)
    - [News](https://www.continuus.ai/news-recognition)
- [Resources](https://blog.continuus-technologies.com/)
- [Contact](https://www.continuus-technologies.com/contact)

[All posts](https://blog.continuus.ai/all)

 July 19, 2024

# How I Used Python to Automate Cleaning 160k Unread Emails

![Picture of Sal Fadel](https://blog.continuus.ai/hs-fs/hubfs/continuus-tech-sept2019-5%20(1).jpg?width=50&name=continuus-tech-sept2019-5%20(1).jpg) By  [Sal Fadel](https://blog.continuus.ai/author/sal-fadel)  ·   1 minute read

[Return to Resources](https://blog.continuus.ai/)

First things first… You’re probably wondering why I had 160,000 unread promotional emails in email account to begin with, and how I let it get that out of control. For the last five years, I have used my Gmail account as my go-to “sign-up” email. Every subscription, promotion, advertisement, and website that I needed to add my email to, I did. To say the least, it got out of hand quickly, and I simply couldn’t keep up. Once I hit 30,000 unread emails, I gave up and figured someday I would figure out a way to automate deleting them.  

Finally, I did just that! If you are anything like me and have tons of unread (and likely useless) emails, I’ll show you how I did it.  

 

## TEMPORARILY CHANGE YOUR GMAIL SECURITY SETTINGS

The **first and most risky step** is changing your Gmail security settings to allow less secure apps to access your Gmail account. To do this, you will want to go to your Google Account Settings, then click Security and then toggle to allow less secure apps**. I highly recommend that after you are done cleaning up your emails, you turn off this setting**.  

![How I Used Python to Automate Cleaning 160k Unread Emails_1_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_1_211019.png?width=465&height=262&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_1_211019.png)

Once that is complete, we can dive into writing the Python script. Here is the process and code I used.

 

## PYTHON SCRIPT PROCESS

**Step 1:** Import imaplib 

![How I Used Python to Automate Cleaning 160k Unread Emails_2_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_2_211019.png?width=344&height=50&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_2_211019.png)

 

** Step 2:** Use imap to connect to Gmail and  authenticate the user 

![How I Used Python to Automate Cleaning 160k Unread Emails_3_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_3_211019.png?width=476&height=98&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_3_211019.png)

 

** Step 3:** Select the mailbox to delete emails from 

![How I Used Python to Automate Cleaning 160k Unread Emails_4_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_4_211019.png?width=552&height=60&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_4_211019.png)

 

**Step 4:** Search and find promotional emails that are not marked important and batch move them to the trash folder 

![How I Used Python to Automate Cleaning 160k Unread Emails_5_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_5_211019.png?width=672&height=314&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_5_211019.png)

 

** Step 5:** Empty trash folder 

![How I Used Python to Automate Cleaning 160k Unread Emails_6_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_6_211019.png?width=668&height=134&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_6_211019.png)

 

 **Step 6:** Logout 

![How I Used Python to Automate Cleaning 160k Unread Emails_7_211019](https://blog.continuus.ai/hs-fs/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_7_211019.png?width=510&height=112&name=How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_7_211019.png)

 

This process worked great and took just over 10 minutes to delete the 160K promotional email. My next evolution is to export all the unsubscribe links at the bottom of the emails and automate the unsubscribe process.  

*Disclaimer: Continuus Technologies and its employees are not responsible for any loss of data or emails as a result of running this script. Please run this script at your own discretion and risk. *

Share: [facebook-f icon](http://www.facebook.com/share.php?u=https://blog.continuus.ai/python-automate-cleaning-unread-emails) [linkedin-in icon](http://www.linkedin.com/shareArticle?mini=true&url=https://blog.continuus.ai/python-automate-cleaning-unread-emails) [twitter icon](https://twitter.com/intent/tweet?url=https://blog.continuus.ai/python-automate-cleaning-unread-emails) [pinterest-p icon](http://pinterest.com/pin/create/link/?url=https://blog.continuus.ai/python-automate-cleaning-unread-emails) [envelope icon](mailto:?body=https://blog.continuus.ai/python-automate-cleaning-unread-emails)

## Related posts

[![](https://blog.continuus.ai/hubfs/businessman%20hand%20working%20with%20modern%20technology%20and%20digital%20layer%20effect%20as%20business%20strategy%20concept-1.jpeg)](https://blog.continuus.ai/enterprise-knowledge-agents-financial-services)

[Blog](https://blog.continuus.ai/tag/blog)

### [Enterprise Knowledge Agents: What Comes After Search](https://blog.continuus.ai/enterprise-knowledge-agents-financial-services)

[![](https://blog.continuus.ai/hubfs/Technician%20walking%20in%20hallway%20of%20server%20room.jpeg)](https://blog.continuus.ai/llms-financial-services)

[Blog](https://blog.continuus.ai/tag/blog)

### [Beyond Chatbots: How Financial Firms Are Using LLMs to Unlock Knowledge](https://blog.continuus.ai/llms-financial-services)

[![](https://blog.continuus.ai/hubfs/2019%20Blog%20Images/Real%20World%20Data%20Visualizations_Milwaukees%20Best%20Restaurants_191023/Real%20World%20Data%20Visualizations_Milwaukees%20Best%20Restaurants_Thumbnail_191023.png)](https://blog.continuus.ai/real-world-data-viz-milwaukee-best-restaurants)

[Blog](https://blog.continuus.ai/tag/blog)

### [Real World Data Visualizations: Milwaukee's Best Restaurants](https://blog.continuus.ai/real-world-data-viz-milwaukee-best-restaurants)

### Pages

---

### Resources

---

[Home](https://www.continuus.ai/)

[Services](https://www.continuus.ai/services)

[Partners](https://www.continuus.ai/partners)

[About](https://www.continuus.ai/about)

 

[Our Team](https://www.continuus.ai/our-team)

[Careers](https://www.continuus.ai/careers)

[News](https://www.continuus.ai/news-recognition)

[Contact](https://www.continuus.ai/contact)

Blog Posts

Case Studies

Solution Briefs

Webinars

Demos

Videos

All

[Cookies Policy](https://www.continuus.ai/cookies-policy) | [Privacy Policy](https://www.continuus.ai/privacy-policy) | [Accessibility](https://www.continuus.ai/accessibility) | *©* [*Continuus Technologies*](https://www.continuus.ai/) *2024*

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Sal Fadel",
    "url" : "https://blog.continuus.ai/author/sal-fadel"
  },
  "dateModified" : "2024-07-19T19:09:12.161Z",
  "datePublished" : "2021-10-20T02:55:00.000Z",
  "headline" : "How I Used Python to Automate Cleaning 160k Unread Emails",
  "image" : [ "https://blog.continuus.ai/hubfs/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.continuus.ai/python-automate-cleaning-unread-emails",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.continuus.ai/hubfs/Continuus%20Logo%202024%20-%20Horizontal%20-%20Full%20Color%20-%20Padded-1.png"
    },
    "name" : "Continuus"
  }
}
```

```json
{
  "@context" : "http://schema.org",
  "@type" : "Article",
  "author" : {
    "@type" : "Person",
    "name" : [ "Sal Fadel" ],
    "url" : "https://blog.continuus.ai/author/sal-fadel"
  },
  "datePublished" : "2021-10-20T02:55:00+0000",
  "description" : "Inbox out of control? Here's a Python script to bulk delete thousands of promotional or junk emails in just 10 minutes.",
  "headline" : "How I Used Python to Automate Cleaning 160k Unread Emails",
  "image" : "https://21082575.fs1.hubspotusercontent-na1.net/hubfs/21082575/2021%20Blog%20Images/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019/How%20I%20Used%20Python%20to%20Automate%20Cleaning%20160k%20Unread%20Emails_211019.png",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://21082575.fs1.hubspotusercontent-na1.net/hubfs/21082575/Continuus%20Logo%202024%20-%20Horizontal%20-%20Full%20Color%20-%20Padded-1.png"
    },
    "name" : "Continuus"
  },
  "url" : "https://blog.continuus.ai/python-automate-cleaning-unread-emails"
}
```