Skip to content

Instantly share code, notes, and snippets.

@kasperhartwich
Last active January 26, 2021 08:10
Show Gist options
  • Save kasperhartwich/dec5923ffcba55686dfb to your computer and use it in GitHub Desktop.
Save kasperhartwich/dec5923ffcba55686dfb 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;
}
@si294r
Copy link

si294r commented Jul 5, 2017

thanks man

@MrBorderline
Copy link

MrBorderline commented Jun 22, 2018

Hi man, i created my token test but, i change this

' abcd-12345678-123456789-12345 \'-3 months\''

for this

'xoxp-39012385831-30631xxxxx483-386021xxxxxxxx7-57f91xxx3x1xxx1x91b09b927dda54d47b \'-3 months\''

`#!/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] . 'xoxp-39012385831-30631xxxxx483-386021xxxxxxxx7-57f91xxx3x1xxx1x91b09b927dda54d47b \'-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; 
}`

print this

~$ php delSlack.php
delSlack.php
Example: delSlack.php39012385831-306315224483-386041219537-57006f911a131191b09b927dda54d47b '-3 months'

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment