Skip to content

Instantly share code, notes, and snippets.

@rudelm
Last active June 3, 2023 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudelm/66f00eee0d2836d166ddaa209bb870e7 to your computer and use it in GitHub Desktop.
Save rudelm/66f00eee0d2836d166ddaa209bb870e7 to your computer and use it in GitHub Desktop.
Mass deletion of old Tweets from Twitter
# this is based on https://pushpullfork.com/i-deleted-tweets/ and http://digitallearning.middcreate.net/tips/deleting-tweets-non-techie-starts-her-personal-information-environmentalism-journey-with-lots-of-help/
import tweepy
import csv
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''
def read_csv(file):
"""
reads a CSV file into a list of lists
"""
with open(file, encoding = 'utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter = ',')
rows = []
for line in reader:
row_data = []
for element in line:
row_data.append(element)
if row_data != []:
rows.append(row_data)
rows.pop(0)
return(rows)
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 = read_csv('./tweets.csv')
tweets_marked = []
month_list = ['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06' '2017-07', '2017-09', '2017-10', '2017-11', '2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06']
for tweet in tweets:
if tweet[3][0:7] in month_list:
tweets_marked.append(tweet)
print(len(tweets_marked), 'tweets marked for deletion.')
for tweet in tweets_marked:
print(tweet[5])
# build list of marked status IDs
to_delete_ids = []
delete_count = 0
for tweet in tweets_marked:
to_delete_ids.append(tweet[0])
# delete marked tweets by status ID
for status_id in to_delete_ids:
try:
api.destroy_status(status_id)
print(status_id, 'deleted!')
delete_count += 1
except:
print(status_id, 'could not be deleted.')
print(delete_count, 'tweets deleted.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment