Skip to content

Instantly share code, notes, and snippets.

@bdw429s
Last active May 22, 2018 11:01
Show Gist options
  • Save bdw429s/72e649a04d1bfba811d224abda1fab2d to your computer and use it in GitHub Desktop.
Save bdw429s/72e649a04d1bfba811d224abda1fab2d to your computer and use it in GitHub Desktop.
This is a CommandBox Task Runner that will delete file from your Slack team that are older than 30 days. Please see the first comment for usage instructions.
/**
* Delete Slack files older than 30 days
*/
component {
function run( token='', user='' ) {
if( !token.len() ) {
error( 'Need an API token provided. Edit this task or pass it as ":token=foobar".' );
}
// Current epoch minus 30 days of seconds
var ts_to = dateDiff( "s", "January 1 1970 00:00", DateConvert("Local2utc", now() ) ) - ( 30 * 24 * 60 * 60);
// Grab list of files
http url='https://slack.com/api/files.list' result='local.response' method='post' {
httpparam type='formField' name='token' value=token;
httpparam type='formField' name='ts_to' value=ts_to;
// Filter on User
if( user.len() ) {
httpparam type='formField' name='user' value=user;
}
httpparam type='formField' name='count' value=1000;
}
// A little error checking for bad auth
if( !deserializeJSON( local.response.fileContent ).keyExists( 'files' ) ) {
print.boldRedLine( 'Error: ' )
.line()
.line( formatterUtil.formatJSON( deserializeJSON( local.response.fileContent ) ) );
return;
}
// Loop over each file
deserializeJSON( local.response.fileContent ).files.each( function( file ) {
print.yellowText( 'Deleting [#file.filetype#] file [#file.title# #file.name#]: ' ).toConsole();
// Delete it (if we have permissions to)
http url='https://slack.com/api/files.delete' result='local.response' method='post' {
httpparam type='formField' name='token' value=token;
httpparam type='formField' name='file' value=file.id;
}
var resp = deserializeJSON( local.response.fileContent );
print.line( resp.ok & ( !resp.ok ? ' - ' & resp.error : '' ) , resp.ok ? 'green' : 'red' ).toConsole();
} );
}
}
@bdw429s
Copy link
Author

bdw429s commented Mar 22, 2018

This is a CommandBox Task Runner that will delete file from your Slack team that are older than 30 days. You will need your Slack API token and your Slack user id to filter only files you own. Slack makes it a bit of a pain to find both of these.
Click https://cfml.slack.com/files open your browser's network debugging, filter on "XHR" and click "next" at the bottom of the page. You will find a form field called "token" in the HTTP request that was made that contains your API token in the format

xoxs-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx

Then click the "My Files" tab and your user Id will be the last portion of the URL after /files/ in the format:

Uxxxxxxxx

Save the file locally as slackFileDelete.cfc and run the CommandBox task like so:

$> box task run slackFileDelete run xoxs-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx Uxxxxxxxx

The user id is optional if you are an account admin. If you are not an account admin and you leave off the user id, you will be unable to delete files you don't own.

Grab the latest CommandBox binary here: https://www.ortussolutions.com/products/commandbox#download

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