Skip to content

Instantly share code, notes, and snippets.

@robbintt
Last active June 9, 2021 06:35
Show Gist options
  • Save robbintt/b6e051fe17f3c41cbae082518856dddd to your computer and use it in GitHub Desktop.
Save robbintt/b6e051fe17f3c41cbae082518856dddd to your computer and use it in GitHub Desktop.
Delete all tweets with tweepy. Sometimes twitter forgets to tell you you have more tweets, so you have to run it later.
''' Bulk delete all your tweets and likes.
Doesn't delete media. Twitter does a terrible job dredging tweets, expect to have to rerun this a few weeks later, I am still not done after my 2nd pass a few weeks later. The "X tweets" seems to be accurate even though none are dispayed.
Export these with a shell script:
export TWITTER_USERNAME=
export TWITTER_CONSUMER_KEY=
export TWITTER_CONSUMER_SECRET=
export TWITTER_ACCESS_TOKEN=
export TWITTER_ACCESS_TOKEN_SECRET=
'''
import os
import time
import tweepy
def setup_tweepy():
consumer_key = os.environ.get('TWITTER_CONSUMER_KEY')
consumer_secret = os.environ.get('TWITTER_CONSUMER_SECRET')
access_token = os.environ.get('TWITTER_ACCESS_TOKEN')
access_token_secret = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
def delete_most_recent_tweets(count=100):
tweepy_api = setup_tweepy()
user = tweepy_api.get_user(USER)
for status in tweepy.Cursor(tweepy_api.user_timeline, count=count, include_rts=True).items():
print(status.text)
resp = tweepy_api.destroy_status(status.id)
time.sleep(0.1)
def delete_most_recent_likes(count=100):
tweepy_api = setup_tweepy()
user = tweepy_api.get_user(USER)
favorites_page = tweepy_api.favorites(id=user.id, count=count)
print(f"Deleting {count} (more) likes...")
try:
for status in favorites_page:
print(status.text)
resp = tweepy_api.destroy_favorite(status.id)
time.sleep(0.1)
except tweepy.error.TweepError:
''' tweepy is trying to delete a response to a non-existent tweet...
pass for now and pick up next time around
'''
return
if __name__ == '__main__':
''' Delete all your tweets...
'''
USER = os.environ.get('TWITTER_USERNAME')
# delete tweets and likes
while True:
try:
count = 100 # default
print(f"Deleting {count} (more) tweets...")
delete_most_recent_tweets(count)
delete_most_recent_likes(count)
time.sleep(1)
except tweepy.error.RateLimitError:
print("Rate limit: sleeping for 1 minutes.")
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment