Last active
August 10, 2018 01:29
-
-
Save purinda/017ae36f0c98d7cb41668b4301109aed to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# Slack files prune script for clearing up space for free slack accounts. | |
# Config | |
TOKEN="<your token>" | |
URL_BASE="https://slack.com/api/" | |
# API endpoints | |
URL_LIST="${URL_BASE}files.list" | |
URL_FILE_DELETE="${URL_BASE}files.delete" | |
get_file_lists() { | |
# Make the first request and figure out how many pages. | |
curl -X POST "${URL_LIST}?token=${TOKEN}" -o files.1.json | |
NUM_PAGES=`jq '.paging.pages' files.1.json` | |
for page in `seq 1 ${NUM_PAGES}`; | |
do | |
curl -X POST "${URL_LIST}?token=${TOKEN}" -F "page=${page}" -o "files.${page}.json" | |
done | |
} | |
delete_files() { | |
# Make an array out of file IDs in JSON data. | |
for page in `seq ${NUM_PAGES} 1`; | |
do | |
FILE_JSON="files.${page}.json" | |
FILE_IDS=`jq '.files[].id' ${FILE_JSON}` | |
FILE_IDS_ARR=(${FILE_IDS//$'\n'/ }) | |
for FILE_ID in "${FILE_IDS_ARR[@]}" | |
do | |
FILE_ID=${FILE_ID//\"} | |
echo "Deleting: ${FILE_ID}" | |
curl -X POST ${URL_FILE_DELETE}?token=${TOKEN} -F "file=${FILE_ID}" | |
echo "" | |
done | |
done | |
} | |
echo "Calling files.list: Loading entire history of files uploaded/shared." | |
get_file_lists | |
echo "Calling files.delete: Deleting batch of files" | |
delete_files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment