Skip to content

Instantly share code, notes, and snippets.

@pbashyal-nmdp
Last active May 11, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbashyal-nmdp/edb65c47404ad49d6ce4025c46f288c5 to your computer and use it in GitHub Desktop.
Save pbashyal-nmdp/edb65c47404ad49d6ce4025c46f288c5 to your computer and use it in GitHub Desktop.
Delete old slack files
#!/bin/bash
# Delete old slack files that are older than 30 days
# Api Token
# Get token from https://api.slack.com/custom-integrations/legacy-tokens
TOKEN=${SLACK_TOKEN?:"No token Provided"}
# 30 Days ago (The -v option is BSD Unix/Mac Specific)
MONTH_BEFORE=`date -v-30d +%s`
FILE_LIST_JSON="/tmp/slack_file_list.json"
# Call the files.list API for file types: images,pdfs,videos
curl -X GET https://slack.com/api/files.list\?token\=${TOKEN}\&ts_to\=${MONTH_BEFORE}\&types=images%2Cpdfs%2Cvideos%2Czips\&pretty\=1 > $FILE_LIST_JSON
# Show the filenames to be deleted
echo "Deleting files:"
grep \"name\"\: ${FILE_LIST_JSON} | cut -d\" -f4 | cat -n
# Get list of file ids
FILE_IDS=`grep \"id\"\: ${FILE_LIST_JSON} | cut -d\" -f4`
# Turn the file_list into an array
IFS=$'\n' file_list=( ${FILE_IDS} )
echo "Deleting Number of files: ${#file_list[@]}"
# Delete file one at a time
for file in ${file_list[@]}; do
echo "Deleting file: $file \n"
# Call the files.delete API
curl https://slack.com/api/files.delete\?token\=${TOKEN}\&file\=${file}
sleep 1.5 # Handle API rate limiting on delete
done
# Remove file list
rm -f ${FILE_LIST_JSON}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment