Skip to content

Instantly share code, notes, and snippets.

@eduardopintor
Forked from kasperhartwich/delete-from-slack.php
Created January 24, 2019 21:19
Show Gist options
  • Save eduardopintor/1a022c58ac1dd42212633293a6166945 to your computer and use it in GitHub Desktop.
Save eduardopintor/1a022c58ac1dd42212633293a6166945 to your computer and use it in GitHub Desktop.
Script to delete old files from Slack
#!/usr/bin/env php
<?php
date_default_timezone_set('GMT');
if (count($argv)<2) {
echo $argv[0] . ' <token> <until>' . PHP_EOL;
echo 'Example: ' . $argv[0] . ' abcd-12345678-123456789-12345 \'-3 months\'' . PHP_EOL;
exit;
}
$token = $argv[1];
$timestamp = isset($argv[2]) ? strtotime($argv[2]) : null;
function slack($function, $options = []) {
global $token;
$response = file_get_contents('https://slack.com/api/' . $function . '?' . http_build_query(array_merge(['token' => $token],$options)));
return json_decode($response, true);
}
$files = slack('files.list', ['ts_to' => $timestamp]);
foreach ($files['files'] as $file) {
$response = slack('files.delete', ['file' => $file['id']]);
echo 'file ' . $file['id'] . ' deleted: ' . $file['permalink'] . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment