‹ Jack's Brain

Twilio SMS Deletion Tool

Dec 03, 2014

**TL;DR **twilio-sms-delete is a tool to iteratively delete all SMS and associated media from your Twilio account.


 

Twilio is a great tool for integrating SIP and SMS into your web applications. It’s got a great, simple interface, and API packages for many of the big languages (Python, PHP, JS, etc.).

When an SMS is sent with media (like a picture or audio; technically an MMS), Twilio makes it available to your application as a publically accessible (but very hard to guess) URL (the actual URL is provided in the JSON of the callback data; no non-text data is given in the callback).

Twilio does NOT auto delete the media or the message (and media storage only goes up to 5GB before you have to start paying), so it’s expected that you delete the media at the least. I suppose the most appropriate time to do this is during your app’s callback – grab the media, process it and store it, then clear out the message. Alternatively, you could queue it for deletion in some kind of async processing queue – regardless, it’s easiest to do when you’re working with one message.

I didn’t get the memo – I just started blithely accepting MMS on my app, processing the file, and moving on… I had lots of stored messages and media, and I was running out of space. How do I go about deleting them?

Media deletion is handled by the restful API. To find a media instance, you must first have a message to locate media for (meaning if you have deleted your message without deleting the media, there doesn’t appear to be any way to get to the media – it’s up, publically, forever — gross, right?). Once you have a message, you can access its list of media. You can then use the message ID AND the media ID (yes, you need both – it’s a parent-child relationship) to delete the media.

There’s barely any web interface to access your messages, so I had to do deletion via the API. In that regard, I whipped up a quick node.js script that would handle all the nitty gritty and horrific nested callbacks to go through all messages, delete all media, then delete the message. This is a simple script, but hopefully this can save someone else the trouble of hacking with an API when all they want to do is clear out stale media.

Check it out on GitHub, or start a conversation on my post in one of reddit’s quietest subs, /r/twilio. MIT license; be forkful and multiply.