Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created August 16, 2021 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BolajiOlajide/7cfaf56b3185fd6f15e7c5b684e35b5e to your computer and use it in GitHub Desktop.
Save BolajiOlajide/7cfaf56b3185fd6f15e7c5b684e35b5e to your computer and use it in GitHub Desktop.
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);
return;
}
console.log('File data is written');
});
#!/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
"""
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(f.read()))
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)):
try:
# Delete tweet
api.destroy_status(tweet_id)
print('[ - ] tweet with id %s deleted' %(tweet_id))
except Exception:
failed_tweets.append(tweet_id)
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:
print(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment