Skip to content

Instantly share code, notes, and snippets.

@blessani
Created February 18, 2020 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blessani/3d10ea096fd1d748e401aa7ec44a4fd1 to your computer and use it in GitHub Desktop.
Save blessani/3d10ea096fd1d748e401aa7ec44a4fd1 to your computer and use it in GitHub Desktop.
PHP script to delete all messages in Slack channel
<?php
class Slack
{
private $token = '';
private $channel = '';
public function __construct()
{
}
public function deleteAllChannelMessages()
{
$data = file_get_contents(sprintf('https://slack.com/api/conversations.history?token=%s&channel=%s', $this->token, $this->channel));
$jsonData = json_decode($data, JSON_PRETTY_PRINT);
foreach ($jsonData['messages'] as $message) {
$this->deleteMessage($this->token, $this->channel, $message['ts']);
printf("Deleting message %s\n", $message['ts']);
}
}
private function deleteMessage($token, $channel, $ts)
{
$url = 'https://slack.com/api/chat.delete';
$fields = [
'token' => urlencode($token),
'channel' => urlencode($channel),
'ts' => urlencode($ts),
];
$fields_string = '';
foreach ($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
}
}
$slack = new Slack();
$slack->deleteAllChannelMessages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment