Delete Messages
Learn to use Slack API calls to delete messages.
In this lesson, we’ll learn to delete regular and scheduled messages in Slack.
Let’s look at the following endpoints in this lesson:
chat.delete
: This endpoint deletes a message.chat.deleteScheduledMessage
: This endpoint deletes a scheduled message.
Delete a message
To delete a message, we access the https://slack.com/api/chat.delete
endpoint. We’ll pass a message’s timestamp, ts
, as a parameter to delete a message. We can use the chat.postMessage
endpoint to send a message and receive the timestamp ts
as a response.
Request parameters
Some important query parameters for the chat.delete
endpoint are as follows:
Parameter | Type | Category | Description |
| token | required | Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter. |
| string | required | This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or a name of a channel. |
| string | required | This specifies the timestamp of the message that needs to be deleted. |
Let’s call the chat.delete
endpoint. Click the “Run” button to send a message to the channel, and delete the same message after ten seconds. Open Slack to see the message before it gets deleted. Once the code widget has completed execution, the message will already have been deleted.
import fetch from "node-fetch";const url = "https://slack.com/api/chat.delete";const headerParameters = {Authorization: "Bearer {{TOKEN}}","Content-Type": "application/json; charset=UTF-8",};// Defining a function to delete the messageasync function deleteMessage() {try {// Sending a message and retrieving its timestampconst timestamp = await sendMessage();await sleep(10000); // Using a custom sleep function to sleep for 10 secondsconst bodyParameters = JSON.stringify({channel: "{{CHANNEL_ID}}",ts: timestamp,});const options = {method: "POST",headers: headerParameters,body: bodyParameters,};const response = await fetch(url, options);printResponse(response);} catch (error) {printError(error);}}deleteMessage();
Let’s look at the highlighted lines in the code widget shown above:
- Line 14: We post a message to the channel and retrieve its timestamp.
- Line 15: We use a custom
sleep
function to halt execution for ten seconds. - Lines 17–20: We define the request body, including the
ts
parameter. - Line 28: We make a call to the
chat.delete
endpoint to delete the posted message.
Response fields
A successful response from this endpoint contains the following fields:
Property | Type | Description |
| boolean | A boolean value indicating whether the request was successful or not. |
| string | The channel from where the message was deleted. |
| string | The Unix timestamp of the deleted message. |
Delete a scheduled message
To delete a scheduled message, we access the https://slack.com/api/chat.deleteScheduledMessage
endpoint. The chat.deleteScheduledMessage
endpoint won’t require the message timestamp, ts
. Instead, we’ll send the message id
of the scheduled message as the scheduled_message_id
parameter. There are two ways to get the scheduled_message_id
parameter. The first is to save the ID from the response of the chat.ScheduledMessage
request. The second is to call the chat.scheduledMessages.list
endpoint and save the id
.
Note: As of now, the
chat.deleteScheduledMessage
can only delete messages that are not due in less than five minutes.
Request parameters
Some important query parameters for the chat.deleteScheduledMessage
endpoint are as follows:
Parameter | Type | Category | Description |
| token | required | Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter. |
| string | required | This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or a name of a channel. |
| string | required | This is the ID returned when a scheduled message is posted. |
Let’s call the chat.deleteScheduledMessage
endpoint. Click the “Run” button to schedule a new message in the first request and delete the scheduled message using the second request.
import fetch from "node-fetch";const url = "https://slack.com/api/chat.deleteScheduledMessage";const headerParameters = {Authorization: "Bearer {{TOKEN}}","Content-Type": "application/json; charset=UTF-8",};// Defining a function to delete the messageasync function deleteScheduledMessage() {try {// Scheduling a message for 6 minutes laterlet message = "Hello World! In 6 minutes";let minutes = 6;const messageId = await scheduleMessage(message, minutes);const bodyParameters = JSON.stringify({channel: "{{CHANNEL_ID}}",scheduled_message_id: messageId,});const options = {method: "POST",headers: headerParameters,body: bodyParameters,};const response = await fetch(url, options);printResponse(response);} catch (error) {printError(error);}}deleteScheduledMessage();
Let’s look at the highlighted lines in the code widget shown above:
- Lines 14–16: We schedule a message for six minutes in the future and retrieve its ID.
- Lines 18–21: We define the request body, including the
scheduled_message_id
parameter. - Line 29: We make a call to the
chat.deleteScheduledMessage
endpoint.
Response fields
The response from this endpoint contains only the ok
field, indicating whether the deletion was successful.