Skip to content

Instantly share code, notes, and snippets.

@EffEPi
Created January 22, 2016 17:23
Show Gist options
  • Save EffEPi/0b98afe93db9f6b77f2f to your computer and use it in GitHub Desktop.
Save EffEPi/0b98afe93db9f6b77f2f to your computer and use it in GitHub Desktop.
Delete Slack files older than 30 days that are not pinned. Rewrite of https://gist.github.com/jackcarter/d86808449f0d95060a40 . get your slack token here: https://api.slack.com/web
<?php
$token = '<YOUR TOKEN GOES HERE>';
#Delete files older than this:
$ts_to = time()-(30*24*3600);
function list_files($token, $ts_to){
$params = [
'token' => $token,
'ts_to' => $ts_to,
'count' => 1000,
];
$uri = 'https://slack.com/api/files.list';
$uri .= '?'.http_build_query($params);
$tmp = file_get_contents($uri);
return json_decode($tmp, true)['files'];
}
function delete_files($token, $files){
$count = 0;
$num_files = count($files);
foreach($files as $file){
$file_id = $file['id'];
$pinned_to = isset($file['pinned_to'])?$file['pinned_to']:[];
if(count($pinned_to)>0){
echo $count.' of '.$num_files." - ".$file_id." not deleted: pinned\n";
}else{
$count ++;
$params = [
'token' => $token,
'file' => $file_id
];
$uri = 'https://slack.com/api/files.delete';
$uri .= '?'.http_build_query($params);
$tmp = file_get_contents($uri);
echo $count.' of '.$num_files." - ".$file_id.' '.json_decode($tmp, true)['ok']."\n";
}
}
}
$files = list_files($token, $ts_to);
delete_files($token, $files);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment