Skip to content

Instantly share code, notes, and snippets.

@Paradoxis
Forked from jackcarter/slack_delete.py
Last active January 30, 2024 17:30
Show Gist options
  • Select an option

  • Save Paradoxis/6ea7b77f5415c7e72a297b1e48cd8be9 to your computer and use it in GitHub Desktop.

Select an option

Save Paradoxis/6ea7b77f5415c7e72a297b1e48cd8be9 to your computer and use it in GitHub Desktop.

Revisions

  1. Paradoxis revised this gist Jul 29, 2016. 1 changed file with 74 additions and 29 deletions.
    103 changes: 74 additions & 29 deletions slack_delete.py
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,80 @@
    import argparse
    import requests
    import time
    import json

    token = ''

    #Delete files older than this:
    ts_to = int(time.time()) - 30 * 24 * 60 * 60

    def list_files():
    params = {
    'token': token
    ,'ts_to': ts_to
    ,'count': 1000
    }
    uri = 'https://slack.com/api/files.list'
    response = requests.get(uri, params=params)
    return json.loads(response.text)['files']

    def delete_files(file_ids):
    count = 0
    num_files = len(file_ids)
    for file_id in file_ids:
    count = count + 1
    params = {
    'token': token
    ,'file': file_id
    }
    uri = 'https://slack.com/api/files.delete'

    def main():
    """
    Entry point of the application
    :return: void
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--token", required=True, help="Specifies the OAuth token used for authentication, created at (https://api.slack.com/docs/oauth-test-tokens)")
    parser.add_argument("-d", "--days", type=int, default=None, help="Delete files older than x days (optional)")
    parser.add_argument("-c", "--count", type=int, default=1000, help="Max amount of files to delete at once (optional)")
    options = parser.parse_args()

    try:
    print "[*] Fetching file list.."
    file_ids = list_file_ids(token=options.token, count=options.count, days=options.days)

    print "[*] Deleting files.."
    delete_files(token=options.token, file_ids=file_ids)

    print "[*] Done"

    except KeyboardInterrupt:
    print "\b\b[-] Aborted"
    exit(1)


    def calculate_days(days):
    """
    Calculate days to unix time
    :param days: int
    :return: int
    """
    return int(time.time()) - days * 24 * 60 * 60


    def list_file_ids(token, count, days=None):
    """
    Get a list of all file id's
    :param token: string
    :param count: int
    :param days: int
    :return: list
    """
    if days:
    params = {'token': token, 'count': count, 'ts_to': calculate_days(days)}
    else:
    params = {'token': token, 'count': count}

    uri = 'https://slack.com/api/files.list'
    response = requests.get(uri, params=params)
    print count, "of", num_files, "-", file_id, json.loads(response.text)['ok']
    files = json.loads(response.text)['files']
    return [f['id'] for f in files]


    def delete_files(token, file_ids):
    """
    Delete a list of files by id
    :param token: string
    :param file_ids: list
    :return: void
    """
    count = 0
    num_files = len(file_ids)
    for file_id in file_ids:
    count += 1
    params = {'token': token, 'file': file_id}
    uri = 'https://slack.com/api/files.delete'
    response = json.loads(requests.get(uri, params=params).text)
    if response["ok"]:
    print "[+] Deleted", count, "of", num_files, "-", file_id, json.loads(response.text)['ok']
    else:
    print "[!] Unable to delete", count, "of", num_files, "-", file_id + ", reason:", response["error"]

    files = list_files()
    file_ids = [f['id'] for f in files]
    delete_files(file_ids)
    if __name__ == '__main__':
    main()
  2. @jackcarter jackcarter revised this gist Jan 4, 2016. No changes.
  3. @jackcarter jackcarter created this gist Jan 4, 2016.
    35 changes: 35 additions & 0 deletions slack_delete.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import requests
    import time
    import json

    token = ''

    #Delete files older than this:
    ts_to = int(time.time()) - 30 * 24 * 60 * 60

    def list_files():
    params = {
    'token': token
    ,'ts_to': ts_to
    ,'count': 1000
    }
    uri = 'https://slack.com/api/files.list'
    response = requests.get(uri, params=params)
    return json.loads(response.text)['files']

    def delete_files(file_ids):
    count = 0
    num_files = len(file_ids)
    for file_id in file_ids:
    count = count + 1
    params = {
    'token': token
    ,'file': file_id
    }
    uri = 'https://slack.com/api/files.delete'
    response = requests.get(uri, params=params)
    print count, "of", num_files, "-", file_id, json.loads(response.text)['ok']

    files = list_files()
    file_ids = [f['id'] for f in files]
    delete_files(file_ids)