Lo-Fi Python

Sep 13, 2020

Delete All Your Tweets with Tweepy and the Twitter API

You may want to download an archive of your tweets before deleting them. I did this and it took about a day to get my archive download.

How To Purge Your Tweet History with Python

  1. Per the Tweepy library documentation, install tweepy with pip. It worked fine in my python 3.8 virtual environment.
pip install tweepy
  1. Sign up for a Twitter Developer account and create an app. I named mine "tweetcleanr".
  2. Find your app under "Projects & Apps". Edit your app's permissions to "Read + Write + Direct Messages".
  3. After you update your permissions, select the "Keys and tokens" tab. Then regenerate new API keys. Then paste them in the below script.
Twitter Dev UX
  1. Save the below script as a python file. In command prompt or terminal, run python delete_tweets.py or whatever you want to name it!
  2. You'll be asked to go to a link and enter an authorization code. Then you'll see your tweets being deleted like pictured below.

delete_tweets.py

I found this Github Gist via Google and updated the print and input statements to Python 3. I also added the traceback module in case you need to debug it. Initially, I received an error telling me to complete step 3 above. I didn't see the error message at first, until adding traceback.print_exc() like you see below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import tweepy
import traceback

"""Delete All Your Tweets - Github Gist by davej
Credit: https://gist.github.com/davej/113241
Ported to Python 3 by Lo-Fi Python: https://lofipython.com/delete-all-your-tweets-with-tweepy-and-the-twitter-api/
"""
CONSUMER_KEY = "get_from_dev_portal"
CONSUMER_SECRET = "get_from_dev_portal"


def oauth_login(consumer_key, consumer_secret):
    """Authenticate with twitter using OAuth"""

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth_url = auth.get_authorization_url()

    verify_code = input(
        "Authenticate at %s and then enter you verification code here > " % auth_url
    )
    auth.get_access_token(verify_code)

    return tweepy.API(auth)


def batch_delete(api):
    print(
        "You are about to delete all tweets from the account @%s."
        % api.verify_credentials().screen_name
    )
    print("Does this sound ok? There is no undo! Type yes to carry out this action.")
    do_delete = input("> ")
    if do_delete.lower() == "yes":
        for status in tweepy.Cursor(api.user_timeline).items():
            try:
                api.destroy_status(status.id)
                print("Deleted:", status.id)
            except Exception:
                traceback.print_exc()
                print("Failed to delete:", status.id)


if __name__ == "__main__":
    api = oauth_login(CONSUMER_KEY, CONSUMER_SECRET)
    print("Authenticated as: %s" % api.me().screen_name)

    batch_delete(api)
Python Script Deleting Tweets

Twitter Cleanse Complete

Twitter has a really slick developer dashboard. Its API combined with the tweepy library got the job done for me. It's great when stuff just works. And it only cost me about 1 hour to complete. Time to start a clean slate. Here's to looking forward.

Supplementary Reading

Tweepy Documentation Tutorial

Twitter's API Tutorials

Twitter Postman Tutorial