2020-Dec-07, Monday

Deleting twitter DMs

2020-Dec-07, Monday 08:15 pm
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;
code )

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.