19 Oct 2010 ebf   » (Master)

HOWTO: Delete all of your tweets in a few steps

@AlineMary and I decided to change our course with Twitter. Our profiles used to be private, restricted to a few known acquaintances. For personal reasons, we decided to use Twitter in a different way, more public and focused on our projects.

But (always a but) we wouldn’t like to lose our followers or to stop following the interesting people we already followed, so deleting and recreating our accounts was out of league. Manually deleting all tweets, well, was too nightmarish (I had ~ 9800 tweets and @AlineMary ~ 600).

I Googled several mass delete services and found a handful – some, well recommended. There was only one minor problem: none of them worked ;)

Always witty and wise, @AlineMary told me: “You used to be a programmer, right?”. Point taken, my dear :-D .

After googling for some simple-to-use APIs (with OAuth support), I found Jeff Miller’s - “Twitter from the command line in Python using OAuth”, in which he demonstrated the very easy-to-use Tweepy library.

After a quick grok at the documentation, and following Jeff’s footsteps, I managed to assemble a very bare-bones app to mass delete all Tweets. Best of all: It worked :)

So, let’s proceed to the HowTo. You will need to follow Jeff’s steps for the OAuth authentication first:

Step 1: Download Tweepy

Tweepy is an awesome Twitter library for Python. Much of this post is based on information I found in the Tweepy documentation.

Download Tweepy from GitHub and install it on your system.

Step 2: Register a new client app with Twitter

Navigate to http://twitter.com/oauth_clients and click on Register a new application. You might have to log in to the Twitter site first, if you’re not already.

Fill in the registration fields as follows:

When finished on the registration page, click Save.

Next page:

Keep this browser window open. We’ll need the information in the next step.

Step 3: Connect the app to your Twitter account

Next, the app needs to be authorized to connect to your account so it can send tweets under your name.

We’ll create a one-off utility script to do this. Save the following Python code as a script on your local system.

#!/usr/bin/env python

import tweepy

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print "ACCESS_KEY = '%s'" % auth.access_token.key
print "ACCESS_SECRET = '%s'" % auth.access_token.secret

Paste the Consumer Key and Consumer Secret from the end of step 2 into this script, replacing the CONSUMER_KEY and CONSUMER_SECRET constants. Then save and run on your system.

You should see a prompt like this:

Please authorize: <URL>
PIN:

Open that URL in your browser. You should see the standard OAuth Twitter connection screen:

Click Allow.

Twitter will then provide you with a PIN code that authenticates the connection between the client app and your Twitter account.

Enter this PIN into the prompt from the Python registration script:

PIN: 2781961

The script will then print out another key/secret pair:

ACCESS_KEY = '124242RCyi3g0cZ4r5BWL047rsh0S0yv5VxAGwTKCOsHAb'
ACCESS_SECRET = 'kaTXiC489qo8y6haTBSlwOqR1syG83tzPG2StdQ'

But the values will be different each time.

Keep this information on your screen because we’ll need it in the next step.

Ok! Thanks Jeff :) . Now, to the destructive part of this post. Muahuahua*.

We will create a very simple Python script that will iterate over your tweets and… well, delete them :)

#!/usr/bin/env python
import sys
import tweepy
import time

CONSUMER_KEY = 'xxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxx'
ACCESS_KEY = 'xxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxx'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

pag  = 1
status  = []

while (1):
	try:
		status  = api.user_timeline(page=pag)
		for update in status:
			print update.id
			print update.text
			api.destroy_status(update.id)
			print "--"
			pag  = pag + 1
	except:
		print "Failure... sleeping for 10 minutes."
		time.sleep(600) # Sleep for 10 minutes and try again.

And that’s it. Execute this Python script (don’t forget to fill your Keys and Secrets, the one you got with Jeff’s steps) and… wait. This app takes a long time to run (mine is still running). But it works 100% :)

Doubts? Comment!

* Sheldon Cooper. :)

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!