mheavers
mheavers

Reputation: 30168

Nodejs Twilio API 3.x - messages.delete(...) is not a function

Was following this tutorial to try and delete sms messages and I can successfully retrieve an individual message via this code:

client.messages(sid).fetch()
  .then((message) => {
    console.log(message);
    if (message.status === "received") {
      client.messages(sid).delete() //failing here
        .then(() => {
          response.send('Message deleted');
        })
        .catch((err) => console.error(err));
    }
  })

The documentation example only shows how to remove the message body, not delete the messgae itself. And when I console log messages, the only methods available are:

{ create: [Function: create],
  each: [Function: each],
  list: [Function: list],
  page: [Function: page],
  getPage: [Function: getPage],
  get: [Function: get] }

A note that I can successfully redact the message body, following the instructions here:

client.messages(sid).update({body: ''})
.then((message) => {
    process.stdout.write(message.body);
    response.send('message deleted');
})

Upvotes: 1

Views: 478

Answers (1)

BitParser
BitParser

Reputation: 3968

I believe the method you are looking for is remove. Modifying your code like below will actually remove the message:

client.messages(sid).fetch().then((m) => {
    if (m.status == "delivered") {
        console.log("It's delivered")
        console.log(m)
        m.remove().then(() => console.log("message removed"))
    }
})

You also need to take care of any media separately, as those are not deleted when deleting a message according to the documentation.

Upvotes: 2

Related Questions