Deleting twitter DMs

2020-Dec-07, Monday 08:15 pm
[personal profile] chebe
I wanted to delete my DMs on twitter. (Just because seeing things makes me remember things about them, and it's plenty crowded in this head already.) I saw you can delete (your copy) of DMs, but only message-by-message. I tried to find 3rd party tools to do it for me, and they have all stopped existing and/or working. So, I hit up the API.

It turns out versions are crucially important. I'm using python3, tweepy 3.9.0, and twitter API 1.1.

Following on from my previous adventures I had to add 'Direct Messages' permission to my app and regenerate the keys.

Actually deleting the DM is the easy part;
api.destroy_direct_message(direct_message.id)
(Just remember you are only deleting your copy of the messages.)

Finding the message/id to delete is the hard bit. The best available is list_direct_messages which lists both sent and received direct messages, but only in the last 30 days. It uses cursors for paging through potentially long list of results, and can be rate limited.

Best go ahead and make sure you have tweepy 3.9.0, because the previous version has a bug that means the cursors aren't handled correctly in this call;
pip3 install tweepy --upgrade

Putting it all together you get something like;
#!/usr/bin/python3

import time
import sys
import os
import tweepy

consumer_key = 'your_key'
consumer_secret = 'your_secret'
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'

# set up OAuth and integrate with API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            # 15 requests per user, per 15mins
            time.sleep(15 * 60)
        except StopIteration:
            print("StopIteration")
            return

def main():
    #print(tweepy.__version__)
    # list_direct_messages, count default 20, max 50 https://developer.twitter.com/en/docs/twitter-api/v1/direct-messages/sending-and-receiving/api-reference/list-events
    for direct_message in limit_handled(tweepy.Cursor(api.list_direct_messages).items()):
        try:
            # DELETE
            api.destroy_direct_message(direct_message.id)
            print("Deleted: ", direct_message.id)
        except:
            print("Failed to delete: ", direct_message.id)
        time.sleep(15)

if __name__=="__main__":
    main()


As I was manually deleting the DMs older than 30 days I noticed that twitter itself is using version 2 of their API, and doing a circuitous lookup via DM conversations. Something to look into when the new API is supported.

But really, all this could have been avoided if twitter only provided a 'Delete Conversation' button in DMs.
(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

Profile

chebe: (Default)
chebe

June 2025

M T W T F S S
      1
23 45678
9101112131415
16171819202122
23242526272829
30      

Expand Cut Tags

No cut tags

Style Credit