Skip to content

Instantly share code, notes, and snippets.

@thesoftwarejedi
Last active December 19, 2023 12:02
Show Gist options
  • Save thesoftwarejedi/d78af9ee12b7f7a9d3e7 to your computer and use it in GitHub Desktop.
Save thesoftwarejedi/d78af9ee12b7f7a9d3e7 to your computer and use it in GitHub Desktop.
delete old slack files
import requests
import json
import calendar
from datetime import datetime, timedelta
#token from https://api.slack.com/web
_token = "YOUR TOKEN HERE"
_domain = "YOUR SUB DOMAIN HERE"
if __name__ == '__main__':
while 1:
files_list_url = 'https://slack.com/api/files.list'
date = str(calendar.timegm((datetime.now() + timedelta(-30))
.utctimetuple()))
data = {"token": _token, "ts_to": date}
response = requests.post(files_list_url, data = data)
if len(response.json()["files"]) == 0:
break
for f in response.json()["files"]:
print "Deleting file " + f["name"] + "..."
timestamp = str(calendar.timegm(datetime.now().utctimetuple()))
delete_url = "https://" + _domain + ".slack.com/api/files.delete?t=" + timestamp
requests.post(delete_url, data = {
"token": _token,
"file": f["id"],
"set_active": "true",
"_attempts": "1"})
print "DONE!"
@fenech
Copy link

fenech commented May 29, 2017

@theapplepastor it looks like you're using python 3, so print is a function. As such, you'll need to use print(stuff) rather than print stuff.

@tomleo
Copy link

tomleo commented Jul 24, 2017

Thanks for this! I just created a similar script using argparse rather than hard coding _token hope this is helpful to people https://gitlab.com/tomleo/SlackFileCleanup/

@nauman555
Copy link

Python: How to delete multiple files from server at once using OVH cloud API ? can you help me please
I want to delete files from the server using the Pycurl or Python request method. I have a list of file names and I want to delete all files from OVH server by comparing name with the List items. I am using OVH cloud API.

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