Skip to content

Instantly share code, notes, and snippets.

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.
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:
• If you are deleting less than 3200 tweets use this:
• 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
• 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:
• An archive of your tweets. Add the path to the tweets.csv file on line 48. More on how to do this here:
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)):
# Delete tweet
print('[ - ] tweet with id %s deleted' %(tweet_id))
except Exception:
print('Done deleting tweets!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment