Skip to content

Instantly share code, notes, and snippets.

@zachleigh
Last active December 5, 2017 03:49
Show Gist options
  • Save zachleigh/6f1f1d1365ad9d3e1d5fd8e67660b404 to your computer and use it in GitHub Desktop.
Save zachleigh/6f1f1d1365ad9d3e1d5fd8e67660b404 to your computer and use it in GitHub Desktop.
Class to delete Slack files via API call
<?php
use GuzzleHttp\Client;
class SlackFileDeleter
{
/**
* Slack API token.
*
* @var string
*/
protected $token = 'your api token';
/**
* Slack domain.
*
* @var string
*/
protected $domain = 'your Slack domain';
/**
* Start time for delete. For example, if set to '-60 days' all files older
* than 60 days will be deleted.
*
* @var string Valid strtotime string.
*/
protected $time = '-60 days';
/**
* Clear the slack file backlog.
*/
public function cleanAction(array $params)
{
$data = [
'token' => $this->token,
'ts_to' => strtotime($this->time),
];
$client = new Client();
$response = $client->request(
'post',
'https://slack.com/api/files.list',
['form_params' => $data]
);
$files = json_decode($response->getBody(), true)['files'];
if ($files != 0) {
foreach ($files as $file) {
echo "Deleting file {$file['name']} ..." . PHP_EOL;
$timestamp = strtotime(time());
$delete_data = [
'token' => $this->token,
'file' => $file['id'],
'set_active' => 'true',
'_attempts' => 1,
];
$client = new Client();
$response = $client->request(
'post',
"https://{$this->domain}.slack.com/api/files.delete?t={$timestamp}",
['form_params' => $delete_data]
);
}
echo "Done!" . PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment