When we talk about our API, sometimes we use the term “REST” without describing what it actually means. When we look under the hood, we find a lots of moving pieces that fit together to make everything work as described.
Realistically, to people who just use Twilio on a day to day basis, those details are not important. The key part developers should understand is “nouns and verbs.”
Four HTTP Verbs Drive REST APIs
The four HTTP verbs that drive REST APIs are POST, GET, PUT, and DELETE. The POST and GET verbs and are well-understood by modern web developers because we use them to create and retrieve our nouns. Less common but just as important is PUT. Ideally, we use it to update our nouns. The fourth verb – DELETE – will delete our noun but is rarely used. Within the Twilio API, we use DELETE in one place: call recordings.
Depending on how you use Twilio, you could easily collect call recordings which you don’t want to store long term. You could delete these via the Dashboard on the site but programmatically using the REST API is more powerful and flexible. It’s even easy if you use our Helper Libraries.
Warning: If you just copy and paste the following code, it will delete all the Recordings within the specified date ranges. Please customize this code before you use it.
In PHP, it’s as simple as authenticating with our Account Sid and AuthToken and then calling the delete method on each Recording. In this sample, I use getIterator to get a specific date range of recordings, display some information, and delete them one by one using their Recording Sid:
Not surpisingly, the equivalent using our Python module is quite similar:
Of course, you can still go with raw cURL from the command line:
Delete Specific Call Recordings
Realistically, we probably don’t want to blindly delete Recordings by date range. We could have important customer calls, voice mail greetings, or other important Recordings. To ensure that the delete focuses exactly on the Recordings we want, we’ll combine a few pieces to make something more useful. In this particular case, first we get a list of Calls filtered by the To phone number and the StartTime. Next, we only delete the call recordings if they are less than 20 seconds. In my account, this To number is used exclusively for testing and development of voice apps, so deleting the shortest call recordings makes sense:
In general, keeping your account “clean” is useful. While everything else within the REST API is text, Recordings are files and large by comparison. If you regularly delete the unimportant files, your processing will be easier and faster.