Skip to content

Instantly share code, notes, and snippets.

@dansanderson
Created September 27, 2018 02:36
Show Gist options
  • Save dansanderson/e16bbc8b3d78aea4e0d427042be4c2a3 to your computer and use it in GitHub Desktop.
Save dansanderson/e16bbc8b3d78aea4e0d427042be4c2a3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
THIS SCRIPT DELETES YOUR TWEETS FROM TWITTER. Be careful! You are solely responsible for any
unintentionally lost data!
Specifically, this reads tweets from a personal data export, then deletes them from Twitter. As written,
this script deletes 1,000 tweets at a time, writing to a log file so it can start where it left off when
you run it again. This is just how I felt comfortable doing it, deleting in batches and eventually
deleting my entire Twitter history and preserve the local export data. Edit to your taste.
Step 1: Apply for a Twitter developer account. https://developer.twitter.com/en/apply/user
This process now requires an application with a statement of intent, etc. but it's not too bad.
Step 2: On the developer site, "Create an app." Find the "Keys and tokens" for the app. You
need a consumer key and secret, and an access token and secret.
Step 3: Install the Python library "tweepy". Using the pip command: "pip install tweepy"
Step 4: Export your Twitter data. When signed in to Twitter, visit your "Settings and privacy,"
"Your Twitter data." Enter your Twitter password. At the bottom next to "Twitter," click "Request
data." Wait for the notification email, then download the file.
Step 5: Edit this script to insert your consumer key and secret, and your access token and secret,
as indicated.
Step 6: Run the script in the same directory as your tweet.js file (or make appropriate changes
to the script).
Good luck!
"""
import dateutil.parser
import json
import os
import tweepy
import sys
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
ACCESS_TOKEN = '...'
ACCESS_TOKEN_SECRET = '...'
LOGFILE_NAME = 'delete_tweets_log.txt'
def main(args):
raw_data = json.load(open('tweet.js'))
ids_and_dates = [(k['id_str'], dateutil.parser.parse(k['created_at']), k['full_text'])
for k in raw_data['tweets']]
ids_and_dates.sort(key=lambda v: v[1])
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
start = 0
if os.path.isfile(LOGFILE_NAME):
with open(LOGFILE_NAME) as logfile:
for line in logfile:
id = line[:-1]
if ids_and_dates[start][0] != id:
print('Log and archive out of sync??')
return 1
start += 1
with open(LOGFILE_NAME, 'a') as logfile:
for i in range(1000):
if len(ids_and_dates) <= start + i:
return 0
id, create_date, txt = ids_and_dates[start + i]
print(f'deleting id:{id} date:{create_date} text: {txt}')
try:
api.destroy_status(id)
except tweepy.error.TweepError as e:
with open('delete_tweets_error_log.txt', 'a') as errorlog:
errorlog.write(f'{id}: {e}\n')
logfile.write(id + '\n')
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment