Skip to content

Instantly share code, notes, and snippets.

@r-gr
Forked from davej/delete_all_tweets.py
Last active December 29, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-gr/7631049 to your computer and use it in GitHub Desktop.
Save r-gr/7631049 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
This script will delete all of the tweets in the specified account.
You may need to hit the "more" button on the bottom of your twitter profile
page every now and then as the script runs, this is due to a bug in twitter.
You will need to get an api key and api secret token to use this
script, you can do so by registering a twitter application at
https://dev.twitter.com/apps
@requirements: Python 2.6+, Tweepy (http://pypi.python.org/pypi/tweepy)
@author: Rory Green, Dave Jeffery
"""
from __future__ import print_function
import tweepy
API_KEY = "XXX"
API_SECRET = "XXX"
def oauth_login(api_key, api_secret):
"""Authenticate with twitter using OAuth. Return api object."""
auth = tweepy.OAuthHandler(api_key, api_secret)
auth_url = auth.get_authorization_url()
print("Authenticate at {}".format(auth_url))
print("and then enter your verification code here", end="")
verify_code = raw_input(" >>> ")
auth.get_access_token(verify_code)
return tweepy.API(auth)
def batch_delete(api):
"""Delete all tweets from account held in api object."""
print("You are about to Delete all tweets from the account @{}.".format(
api.verify_credentials().screen_name))
print("Does this sound ok? There is no undo!")
print("Type yes to carry out this action.", end="")
do_delete = raw_input(" >>> ")
if do_delete.lower() == "yes":
failed = []
for status in tweepy.Cursor(api.user_timeline).items():
try:
api.destroy_status(status.id)
print("Deleted:", status.id)
except:
print("Failed to delete:", status.id)
failed.append(status)
while failed:
for i, status in enumerate(failed):
print("Retrying:", status.id)
try:
api.destroy_status(status.id)
print("Deleted:", status.id)
del failed[i]
except:
print("Failed to delete:", status.id)
if __name__ == "__main__":
api = oauth_login(API_KEY, API_SECRET)
print("Authenticated as: {}".format(api.me().screen_name))
batch_delete(api)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment