Deleting all your Tweets

A couple of months ago, I decided to start fresh on my Twitter account. I didn’t want to lose the people I’d been following, so I decided I’d rather just get rid of my tweets instead of recreating my account. I went looking, and found a website that claimed to delete all my tweets – I signed in and tried it, but it only got through the first couple thousand. After some research, I discovered that this was by design on the part of Twitter – you can’t retrieve tweets via the API after a certain point. Most services I found were charging between $30-50 to delete the rest of your tweets.

I decided that seemed like a ripoff so went looking for something else, and stumbled across Kris Shaffer’s post¬†outlining how to just do it using Python, which then led me back even further to Dave Jeffery’s 2014 Gist to delete all tweets in an account. Since that Gist was published, Twitter has changed their archive download format to a JSON file, so I wanted to walk through how to do this with a current Twitter archive, as well as how to create filtering criteria within the script. You can follow all the directions in Kris’s post as far as creating an application on Twitter and downloading your Twitter archive, but you’ll notice that inside your Twitter archive you now have tweet.js instead of tweets.csv. No big deal, JSON’s fairly easy to handle with Python. Open up tweet.js and remove window.YTD.tweet.part0 = from the first line, then save and close.

Make sure you run “pip install tweepy” to make sure you’ve got the Tweepy package ready to go – otherwise the script won’t run.

Now, you can just use the below script to wipe out your past tweets, replacing the blank variables with thekeys from your Twitter application. I’ve annotated the important bits fairly heavily to explain how the whole thing works and how you can modify the script to delete based on your own criteria.

import json
from dateutil.parser import parse
import tweepy

consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''

# These lines authenticate against the API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
print("Authenticated as: %s" % api.me().screen_name)

tweets = []

with open("tweet.js", encoding="utf8") as f:
  tweets = json.load(f) # Python's pretty good at JSON! One line to just load the entire set of Tweets into a Python list.

tweets_to_delete = []
delete_count = 0
for tweet in tweets:

  # This is where you'll create your criteria for deleting a tweet. Here, I'm setting it to "delete everything before November 2018", but you can do whatever.
  # For example, you could do something along the lines of
  # for hashtag in tweet["entities"]["hashtags"]:
    # if hashtag["text"] = "#hashtag":
  # ...to delete Tweets with a specific hashtag. The dataset is actually super robust, so there's all sorts of stuff you can do, from searching Tweets that mention a specific user
  # to deleting based on a search of the Tweet text.
  # Either way, you'll want to make sure you append to the "tweets_to_delete" array after you set up your criteria.

  create_date = parse(tweet["created_at"])
  if create_date.year < 2018 or create_date.month < 11:
    tweets_to_delete.append(tweet["id"])
  print(len(tweets_to_delete), ' tweets.')

# This is the part that actually does the delete. You can comment it out if you want to just see how many Tweets you're going to delete first.

for tweet in tweets_to_delete:
  try:
    api.destroy_status(tweet)
    print(tweet, ' deleted!')
    delete_count += 1
  except:
    print(tweet, ' could not be deleted!')

# if you want to delete everything, get rid of the last two for blocks and just uncomment the following:
# for tweet in tweets:
  # try:
    # api.destroy_status(tweet["id"])
    # print(tweet, ' deleted!')
    # delete_count += 1
  # except:
    # print(tweet, ' could not be deleted!')
print(delete_count, 'tweets deleted!')

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.