2

I'm using python-twitter in my Web Application to post tweets like this:

import twitter
twitter_api = twitter.Api(
    consumer_key="BlahBlahBlah",
    consumer_secret="BlahBlahBlah",
    access_token_key="BlahBlahBlah",
    access_token_secret="BlahBlahBlah",
)
twitter_api.PostUpdate("Hello World")

How do I delete all tweets that have been posted? I can't find documentation how to do it.

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

2 Answers2

3

twitter_api.PostUpdate("Hello World") should return a Status object. That Status object also contains information about the status which, according to their source is present as an attribute.

twitter_api.destroyStatus is apparently the method they have which wraps around the POST statuses/destroy twitter request. To destroy a status, it takes as an argument the status.id.

So:

status = twitter_api.PostUpdate("hello world")
twitter_api.destroyStatus(status.id)

should be sufficient. There doesn't seem to be a way to bulk-delete content, you'll have to fetch the content first and then delete it status-by-status.

Fetching a sequence (which I guess implies it is iterable) from your timeline is done with twitter_api.GetUserTimeline with a limit of 200 tweets each time. This should allow you to grab tweets, check if the there's a result and if iterate through them and delete them with destroyStatus.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • How do I fetch all the tweets posted in this Twitter account? Please note, they might not have been posted by my python application. They might have previously been posted through some other Twitter client. – Saqib Ali Jul 09 '16 at 22:44
  • @SaqibAli apparently that seems to be available with [`twitter_api.GetUserTimeline`](https://github.com/bear/python-twitter/blob/master/twitter/api.py#L635), it seems you can get a maximum of `count` tweets each time (where `count == 200`). Then you can iterate through them and destroy them. – Dimitris Fasarakis Hilliard Jul 10 '16 at 10:39
  • When you iterate through the 200 tweets it will go much faster if for each tweet you start a new thread and delete the tweet in its own thread. – Jonas Sep 09 '17 at 03:29
0
import time
import re
import twitter
try:
    # UCS-4
    HIGHPOINTS = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
    # UCS-2
    HIGHPOINTS = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')

class TwitterPurger():
    '''
    Purges the a Twitter account of all all tweets and favorites
    '''
    MAX_CALLS_PER_HOUR = 99
    SECONDS_IN_AN_HOUR = 3600
    def __init__(self):
        self.api_call_count = 0

    def increment_or_sleep(self):
        '''
        Increments the call count or sleeps if the max call count per hour
        has been reached
        '''
        self.api_call_count = self.api_call_count + 1
        if self.api_call_count > TwitterPurger.MAX_CALLS_PER_HOUR:
            time.sleep(TwitterPurger.SECONDS_IN_AN_HOUR)
            self.api_call_count = 0

    def delete_everything(self, screen_name, consumer_key,
                          consumer_secret, access_token, access_token_secret):
        '''
        Deletes all statuses and favorites from a Twitter account
        '''
        api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
                          access_token_key=access_token, access_token_secret=access_token_secret)

        var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
        self.increment_or_sleep()

        while len(var_time_line_statuses) > 0:
            for status in var_time_line_statuses:
                print('Deleting status {id}: {text}'
                      .format(id=str(status.id),
                              text=HIGHPOINTS.sub('_', status.text)))
                api.DestroyStatus(status.id)
            var_time_line_statuses = api.GetUserTimeline(screen_name=screen_name, include_rts=True)
            self.increment_or_sleep()

        user_favorites = api.GetFavorites(screen_name=screen_name)
        self.increment_or_sleep()

        while len(user_favorites) > 0:
            for favorite in user_favorites:
                print('Deleting favorite {id}: {text}'
                      .format(id=str(favorite.id),
                              text=HIGHPOINTS.sub('_', favorite.text)))
                api.DestroyFavorite(status=favorite)
            user_favorites = api.GetFavorites(screen_name=screen_name)
            self.increment_or_sleep()

Windows users will need to run the command "chcp 65001" in their console before running the script from the command prompt.

WithMetta
  • 381
  • 1
  • 5