Last active
May 11, 2018 17:01
-
-
Save pbashyal-nmdp/edb65c47404ad49d6ce4025c46f288c5 to your computer and use it in GitHub Desktop.
Delete old slack files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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