If you want to clean up your Gmail inbox by deleting emails containing the word "unsubscribe", you can either use Gmail filters (easy, no coding) or a Python script with IMAP (for more control). Here’s how:
Method 1: Using Gmail Filters (No Coding Needed) Gmail has built-in filters that can automatically delete emails containing specific words.
Steps: - Open Gmail and click on the search bar at the top.
- Type: unsubscribe and hit Enter to see all emails that contain "unsubscribe."
- Click on the filter icon (looks like three sliders) next to the search bar.
- In the "Has the words" field, type: unsubscribe
- Click Create filter.
- Check "Delete it" (this moves the emails to Trash).
- Click Create filter again.
?? Note: Gmail only applies filters to new incoming emails, so to delete old ones, manually search "unsubscribe", select all, and delete.
Method 2: Using Python + IMAP to Delete Emails If you want to delete existing emails in bulk, you can use Python with IMAP.
Steps to Run the Script - Enable IMAP in Gmail:
- Go to Gmail Settings.
- Under IMAP Access, select Enable IMAP.
- Click Save Changes.
- Generate an App Password (Required if 2FA is enabled):
- Go to Google App Passwords.
- Select Mail and Device ? Click Generate.
- Copy the password (you will need it for the script).
- Run the Python Script:
- Install required libraries if you haven’t: pip install imapclient email
- Use this Python script:
import imaplib import email # Gmail IMAP Settings IMAP_SERVER = "imap.gmail.com" EMAIL_ACCOUNT = "your_email@gmail.com" EMAIL_PASSWORD = "your_app_password" # Use App Password if 2FA is enabled # Connect to Gmail mail = imaplib.IMAP4_SSL(IMAP_SERVER) mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD) mail.select("inbox") # Select the inbox folder # Search for emails containing "unsubscribe" status, messages = mail.search(None, 'BODY "unsubscribe"') if status == "OK": for num in messages[0].split(): mail.store(num, "+FLAGS", "\\Deleted") # Mark emails for deletion # Permanently delete emails mail.expunge() mail.close() mail.logout() print("Emails containing 'unsubscribe' have been deleted!") Which Method is Best? ?? Use Gmail filters (Method 1) if you just want to prevent future spam. ?? Use the Python script (Method 2) if you want to bulk-delete old emails automatically.
If you want to clean up your Yahoo Mail inbox by filtering and deleting emails that contain the word "unsubscribe," you can create a Yahoo Mail filter or use IMAP with a Python script.
Method 1: Using Yahoo Mail Filters (No Code Needed) Yahoo Mail has a built-in filtering system that allows you to delete emails automatically.
- Log in to your Yahoo Mail account.
- Click on the Settings (??) icon in the top right.
- Select More Settings.
- Click Filters on the left panel.
- Click Add new filter.
- Give the filter a name (e.g., "Unsubscribe Cleanup").
- Under Set rules, choose:
- "Contains" and type unsubscribe.
- Under Choose a folder to move to, select Trash (or create a new folder).
- Click Save.
Now, all incoming emails with "unsubscribe" in the content will be moved to Trash.
Method 2: Using Python + IMAP to Delete Emails If you want more control, you can use Python with IMAP to scan and delete emails automatically.
Steps to Run the Script - Enable IMAP in Yahoo Mail:
- Go to Settings (??) > More Settings > Mailboxes.
- Click on your Yahoo Mail account and enable IMAP.
- Generate an App Password (Yahoo requires this for third-party apps):
- Go to https://login.yahoo.com/account/security.
- Click Generate app password.
- Select Other and copy the password.
- Use the following Python script:
python
CopyEdit
import imaplib import email # Yahoo Mail IMAP Settings IMAP_SERVER = "imap.mail.yahoo.com" EMAIL_ACCOUNT = "your_email@yahoo.com" EMAIL_PASSWORD = "your_app_password" # Connect to Yahoo Mail mail = imaplib.IMAP4_SSL(IMAP_SERVER) mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD) mail.select("inbox") # Search for emails with "unsubscribe" status, messages = mail.search(None, 'BODY "unsubscribe"') if status == "OK": for num in messages[0].split(): mail.store(num, "+FLAGS", "\\Deleted") # Permanently delete flagged emails mail.expunge() mail.close() mail.logout() print("Unsubscribe emails deleted!") |