Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2021 01:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
2021 Delete Tweets Script using twitter's export tool
const fs = require('fs');
const tweet_1 = require('./data/tweet.js');
const tweet_2 = require('./data/tweet-part1.js');
const totalTweets = [...tweet_1, ...tweet_2].map(({ tweet }) => ({
id: tweet.id_str,
createdAt: tweet.created_at
fs.writeFile('./tweets.json', JSON.stringify(totalTweets, null, 2), err => {
if (err) {
console.log('File read failed:', err);
console.log('File data is written');
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:
from datetime import datetime, timedelta
import pytz
import json
import tweepy
# 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')
json_obj = []
with open('./tweets.json') as f:
tweets_list = list(json.loads(
days_to_keep = 400 # 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)
failed_tweets = []
for tweet in tweets_list:
tweet_id = tweet.get('id') # tweet[0] is the tweet_id column
tweet_timestamp = datetime.strptime(tweet.get('createdAt'), "%a %b %d %H:%M:%S %z %Y") # tweet[3] is the timestamp column
print(f'processing tweet with ID {tweet_id}')
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(f"An error occurred with tweet with ID: {tweet_id}")
print('Done deleting tweets!')
print("Here's the list of tweets that didn't get deleted")
for t in failed_tweets:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment