How To Delete 8,000+ Emails Using Ruby+Watir

I recently got stuck working on a small browser automation project and needed to continue to build something, so I looked at a few problems I was having. I have an older Yahoo Mail account and although I don’t use it anymore, I still wanted to find a way to clean up the inbox that had a little over 8,000 emails. Doing this by hand would take forever!

Using the same stack from my previous project, I started coding.

I used Ruby and the gem Watir(Webdriver) for the browser automation. Essentially, when the program was run through my terminal, it would go to https://mail.yahoo.com/, log me in, highlight all emails, delete and then repeat for 100 times or until it ran out of emails to delete.

It took me a bit longer than it should have to code this, but it was a blast playing with!

Here’s the code:

require 'watir'
require 'watir-webdriver'
require 'colorize'
b = Watir::Browser.new
b.goto 'https://mail.yahoo.com'
b.text_field(:id => 'login-username').set 'USERNAME'
b.text_field(:id => 'login-passwd').set 'PASSWORD'
b.button(:name => 'signin').click

100.times do
c = b.checkbox :title => ‘Select or deselect all messages [Cmd+A]’
c.exists?
c.set
b.button(:id => ‘btn-delete’).click
b.button(:id => ‘okModalOverlay‘).click
puts (“Successfully Deleted 50 Emails…”).yellow
end

I got a little creative with my selectors, but it worked and I got it functional in no time. I have it set up so that every time it successfully runs the loop it puts “Successfully Deleted 50 Emails…” in yellow within the console so I can watch my terminal and do other things while it ran. You can change how many times it runs depending on how many emails you need to delete. I had my settings to 50 emails per page, although I noticed it would at times delete more than 50 so it didn’t make sense to do the math. Instead, just run a large amount of times until the code fails and stops running.

*I would love to see someone with more skills than mine take this code and make it better!

Here’s It In Action:

 

Building It Out

 

Although this is a super simple project, I want to test my skills a bit more by adding a few features and functions, such as: Showing how many emails are actually in your inbox in console, wrapping this within a GUI using something like Shoes so I can share this with other people. You can put in your username/password and it will clean up your email for you.

 

Probably not the most valuable thing I’ve ever built, but very useful when you need some inbox spring cleaning.

 

Someone recently said they shouldn’t learn coding because they aren’t in a technical field, but that’s the complete wrong way to look at coding/hacking. Coding is a utility/learnable skill to help make your life 10x easier. Case in point; deleting 8,000+ emails!

Dillon Carter

Hey there! I'm a young entrepreneur who blogs about my personal experiences, hacks and lifestyle while also teaching people how to begin working with virtual assistants.