How to Bulk Delete Your Twitter Followers

This post starts out with a little bit of an embarrassing story, followed with a script you can use to delete everyone you are following on twitter.  A couple years ago, I signed up for twitter because hey, someone once squatted my liyanage@hotmail.com email address and I wanted to reserve my twitter name.  I had no interest in actually doing twitter.  I just wanted the virtual real estate (and why isn’t that virtual estate?).  Fast forward a couple years, and I check out my twitter account, and low and behold I am following close to 2,000 people.  Someone had hacked my account, and was selling my following off to the masses.  Unfortunately, there is no bulk delete option on twitter, and it would take an incredibly long time to delete that many people.

I looked at a couple services like socialoomph, and these tools seemed like serious overkill  for what I wanted.  In addition I figured I could be cheap, and hack it myself with their API.  I dusted off my python vagrant box that I set up earlier this summer and got at it.  Here is a rough outline of how you could do this yourself.

  1. Get a Python Server
    1. Don’t have one?  You can use mine like this
      1. Install VirtualBox
      2. Install Vagrant
      3. Grab my Python box from github (https://github.com/BenLiyanage/python-dev-bootstrap/)
  2. Install this twitter API wrapper:
    1. https://code.google.com/p/python-twitter/
    2. Installation instructions are on their site.
  3. Register an Application with Twitter
    1. Login to Twitter here: https://apps.twitter.com/
    2. Click “Create New App”
    3. Click the “API Keys” tab.
    4. Click “Generate My Access Token”
  4. Grab the following codes from the above page, and drop them into the below script
    1. Access token
    2. Access token secret
    3. API key
    4. API secret

 

Here’s the script:

<pre>#!/usr/bin/python
import twitter

# Replace these values with the right keys for your application, and user
api = twitter.Api(
consumer_key='b23FnkffFlzz20sSAstUdoFxG',
consumer_secret='HHQgoXl3otqdbmvifitedZpWzbiaGw1DYY5sqUmic9TW4SXvqT',
access_token_key='490826867-s61pgHrlvHrCXEwmCirpNlVWrMvRR6jjYZX6CX5Z',
access_token_secret='6doHGC9O98vVHTEvmPkCMv5fe53YHzcUfODIaDZrtNaRq'
)

def DeleteUserNoFailing(id):
try:
result = api.DestroyFriendship(id)
return result
except Exception as e:
if e == "Rate limit exceeded":
#wait 5 minutes and try again
sleep(300)
DeleteUserNoFailing(id)
else:
print "failed: " + str(e)
return ""

me = api.VerifyCredentials()
users = api.GetFriendIDs()

for id in users:
print "Deleting " + str(id)
result = DeleteUserNoFailing(id)</pre>

You may note I put in a little bit of code for rate limiting, since I saw some stuff in the twitter documentation about not exceeding 100 requests per hour.  In the end, this was over engineering at its finest as I was able to do my 2000 requests over the span of about 10 minutes.

Its also worth noting that my personal api key has been revoked–you don’t get to hack into my twitter account so soon.

August 27, 2014 • Tags: , , • Posted in: Technology

Leave a Reply