Skip to content

Instantly share code, notes, and snippets.

@marydn
Last active September 26, 2018 14:58
Show Gist options
  • Save marydn/b82b483cdb858bbaf6b6fec4dc37e4e4 to your computer and use it in GitHub Desktop.
Save marydn/b82b483cdb858bbaf6b6fec4dc37e4e4 to your computer and use it in GitHub Desktop.
Delete tweets programmatically. You need to install pip install tweepy.
#!/usr/bin/python
from __future__ import print_function
import tweepy
import itertools
import datetime
# OAuth application
consumer_key = "<<CONSUMER_KEY>>"
consumer_secret = "<<CONSUMER_SECRET>>"
# OAuth account
access_token = "<<ACCESS_TOKEN>>"
access_token_secret = "<<ACCESS_TOKEN_SECRET>>"
# Delete tweets older than this ID
max_id = 807213779268358144
dry_run = False
# in case you want to filter status by dates
startDate = datetime.datetime(2016, 1, 1, 0, 0, 0)
endDate = datetime.datetime(2016, 12, 31, 0, 0, 0)
# authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
screen_name = api.verify_credentials().screen_name
def get_statuses_by_dates():
tweets = []
tmpTweets = tweepy.Cursor(api.user_timeline).items()
for tweet in tmpTweets:
if tweet.created_at >= startDate and tweet.created_at <= endDate:
tweets.append(tweet)
return tweets
def get_statuses_by_max_id():
return tweepy.Cursor(api.user_timeline, max_id = max_id).items()
def print_status(tweet):
print("Deleted: ", tweet.id, " - Date: ", tweet.created_at, " - ", tweet.text.encode('utf8'))
def delete_status(tweet):
try:
if dry_run == False:
api.destroy_status(tweet.id)
print_status(tweet)
except:
print("Failed to delete: ", tweet.id)
print("Deleting tweets for: @", screen_name)
for status in get_statuses_by_dates():
delete_status(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment