Bulk delete of emails in gmail 👆

I had 18 000 emails in my gmail account and tried all sorts of tricks to delete them in bulk. Nothing worked, so I turned to selenium IDE and that worked like a charm. Here is how I did it…

Shaun Enslin
4 min readOct 16, 2022
Photo by Steve Jewett on Unsplash

Nothing worked

Well, I tried all the tricks stack overflow had to offer:

  1. Adding a filter to delete old messages
  2. Searching for old messages, then select all conversations and delete

However, none of the above actually went and deleted the thousands of old emails I no longer needed. Faced with the doomsday possibility of taking 2 days to manually search, select and delete, I needed to come up with an alternative.

I have used selenium and decided to try using the selenium IDE browser extension. After a little tweaking, it performed fantastically and after a couple hours, my gmail was cleaned up.

Install selenium IDE chrome extension

Well, step 1 is to head over to the chrome web store and install the selenium IDE. You can be using either chromium, chrome, brave or edge

https://chrome.google.com/webstore/search/selenium%20IDE

Once installed, you will see it under your extensions.

Save your selnium script file

Copy below file and use your favourite text editor to save it to your desktop, calling it clearhistory.side

{
"id": "ce05ad85-9f99-4308-9ba4-121ed257b116",
"version": "2.0",
"name": "clearhistory",
"url": "https://mail.google.com",
"tests": [{
"id": "081774f4-6fa5-475f-8f9e-ea3877ac4e05",
"name": "Clear history",
"commands": [{
"id": "8a58bd33-f13b-47e7-809c-06d36ae6395b",
"comment": "",
"command": "open",
"target": "/mail/u/0/",
"targets": [],
"value": ""
}, {
"id": "1f3095fc-c8ab-4ae2-9c13-6ba0e8198c9e",
"comment": "",
"command": "setWindowSize",
"target": "1285x834",
"targets": [],
"value": ""
}, {
"id": "7ca90aef-f033-4d5b-bc5f-413fc2fbbaea",
"comment": "",
"command": "times",
"target": "40",
"targets": [],
"value": ""
}, {
"id": "6a6f6401-b96e-4b33-bebd-0b00b94e61a7",
"comment": "",
"command": "click",
"target": "name=q",
"targets": [
["name=q", "name"],
["css=#gs_lc50 > .gb_df:nth-child(1)", "css:finder"],
["xpath=//input[@name='q']", "xpath:attributes"],
["xpath=//div[@id='gs_lc50']/input", "xpath:idRelative"],
["xpath=//div/input", "xpath:position"]
],
"value": ""
}, {
"id": "25bbda7e-8426-4f72-b5b7-6518f744ab5e",
"comment": "",
"command": "type",
"target": "name=q",
"targets": [
["name=q", "name"],
["css=#gs_lc50 > .gb_df:nth-child(1)", "css:finder"],
["xpath=//input[@name='q']", "xpath:attributes"],
["xpath=//div[@id='gs_lc50']/input", "xpath:idRelative"],
["xpath=//div/input", "xpath:position"]
],
"value": "older_than:3y"
}, {
"id": "a2c7cd99-5fcd-4536-9792-935ba41b8954",
"comment": "",
"command": "sendKeys",
"target": "name=q",
"targets": [
["name=q", "name"],
["css=#gs_lc50 > .gb_df:nth-child(1)", "css:finder"],
["xpath=//input[@name='q']", "xpath:attributes"],
["xpath=//div[@id='gs_lc50']/input", "xpath:idRelative"],
["xpath=//div/input", "xpath:position"]
],
"value": "${KEY_ENTER}"
}, {
"id": "66c20fc2-f590-4ce8-9b8f-b7354eb93341",
"comment": "",
"command": "waitForElementNotVisible",
"target": "css=.v1",
"targets": [],
"value": "60000"
}, {
"id": "176ba447-f455-4f54-98eb-6996d36a81f5",
"comment": "",
"command": "click",
"target": "css=.T-Jo-JW",
"targets": [
["css=.T-Jo-JW", "css:finder"],
["xpath=//div[@id=':o4']/div/span", "xpath:idRelative"],
["xpath=//div[2]/div[2]/div/div/div/div/div/div/span", "xpath:position"]
],
"value": ""
}, {
"id": "d6c978c9-7845-4980-b60c-7696546d9711",
"comment": "",
"command": "click",
"target": "css=.T-I-JW > .asa",
"targets": [
["css=.T-I-JW > .asa", "css:finder"],
["xpath=//div[@id=':4']/div[2]/div[2]/div/div/div/div[2]/div[3]/div", "xpath:idRelative"],
["xpath=//div[2]/div/div/div/div[2]/div[3]/div", "xpath:position"]
],
"value": ""
}, {
"id": "68347a25-0891-4baa-ad64-acea3c95c6a8",
"comment": "",
"command": "click",
"target": "css=.v1",
"targets": [
["css=.v1", "css:finder"],
["xpath=//div[8]/div/div/div[2]/div/div/span", "xpath:position"],
["xpath=//span[contains(.,'Loading...')]", "xpath:innerText"]
],
"value": ""
}, {
"id": "b053247d-87bd-4d8d-bac6-ef09ff77a1ed",
"comment": "",
"command": "waitForElementNotVisible",
"target": "css=.v1",
"targets": [],
"value": "60000"
}, {
"id": "b3c2b677-caa2-4e9b-945a-bfe4efb79c8d",
"comment": "",
"command": "click",
"target": "css=.gb_pf path:nth-child(1)",
"targets": [
["css=.gb_pf path:nth-child(1)", "css:finder"]
],
"value": ""
}, {
"id": "af1d1d73-70ac-4daf-a8cc-a7005ede795d",
"comment": "",
"command": "end",
"target": "",
"targets": [],
"value": ""
}]
}],
"suites": [{
"id": "bfa5f3fa-5b40-4a79-a156-decbe2344b92",
"name": "Default Suite",
"persistSession": false,
"parallel": false,
"timeout": 300,
"tests": ["081774f4-6fa5-475f-8f9e-ea3877ac4e05"]
}],
"urls": ["https://mail.google.com/"],
"plugins": []
}

Load the selenium script

Open selenium IDE and open the script you save above.

The script will repeat 40 times (you can change this) and will

  1. Search for emails older than3 years
  2. Wait for the loading bar to disappear
  3. Select all messages
  4. Click on delete
  5. Wait for the loading bar to disappear

It will then repeat this process 40 times.

The results

I had to leave this running for a couple hours to delete all 18 000 old messages, but it did do the job nicely. As seen below, you can track that selenium is doing by simply looking at the browser and tracking the automated tool as it deletes the emails.

--

--