Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created July 16, 2021 07:46
Show Gist options
  • Save BolajiOlajide/f4a1620c256aa5577d031fcf85df43df to your computer and use it in GitHub Desktop.
Save BolajiOlajide/f4a1620c256aa5577d031fcf85df43df to your computer and use it in GitHub Desktop.
#!/bin/python3
"""
A Python script to delete tweets.
• It uses tweet ids from a Twitter archive to bypass the Twitter API limit. Use this script if you need to delete more
than 3200 tweets and what's in your archive is a tweets.csv file.
• If you have more than 3200 tweets but your archive has a tweets.json file use this:
https://gist.github.com/flesueur/bcb2d9185b64c5191915d860ad19f23f
• If you are deleting less than 3200 tweets use this:
https://www.mathewinkson.com/2015/03/delete-old-tweets-selectively-using-python-and-tweepy
• This script is built upon the above 2 scripts, please note I haven't ran any of the above should
there be issues.
• Script runs on Python3.
• To preserve more recent tweets, specify the number of days to keep on line 55. To preserve specific tweets in the
to delete category add them to the tweets_to_keep list on line 56
Requires:
• tweepy installed
• The following credentials from your Twitter app: consumer_key, consumer_secret, access_token and access_token_secret. More
on how to create a Twitter app here: https://developer.twitter.com/en/docs/basics/apps/overview.html
• An archive of your tweets. Add the path to the tweets.csv file on line 48. More on how to do this here:
https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive
"""
import tweepy
from datetime import datetime, timedelta
import csv
import pytz
# Keys
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
print('Authorization request sent')
api = tweepy.API(auth)
print('Authorization successful, loading tweets archive')
with open('/path/to/tweets.csv', 'r') as f:
reader = csv.reader(f)
next(reader, None) # Skip header
tweets_list = list(reader) # Convert CSV to list
days_to_keep = 903 # number of days you want to keep, if you want to keep tweets from the last year the number will be 365
tweets_to_keep = [] # ids of tweets you don't want to delete
cutoff_date = datetime.utcnow() - timedelta(days=days_to_keep)
print('Deleting tweets...')
for tweet in tweets_list:
tweet_id = tweet[0] # tweet[0] is the tweet_id column
tweet_timestamp = datetime.strptime(tweet[3], "%Y-%m-%d %H:%M:%S %z") # tweet[3] is the timestamp column
print(tweet_timestamp, '<', cutoff_date)
if (tweet_id not in tweets_to_keep and tweet_timestamp < cutoff_date.replace(tzinfo=pytz.UTC)):
try:
# Delete tweet
api.destroy_status(tweet_id)
print('[ - ] tweet with id %s deleted' %(tweet_id))
except Exception:
pass
print('Done deleting tweets!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment