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!"
@svpino
Copy link

svpino commented Aug 20, 2015

Awesome! Thanks for sharing! I'll update the post to point to this Gist.

@morales2k
Copy link

Greetings! I have also implemented this code in my workplace. It is great help! I have found an inconvenience with file name encoding issues that cause the script to stop on some files and I am forced to look said files up and manually delete them.

An example filename that gave me such issue is this:
kia_quiz___kia_puerto_rico_png_1_591\ xd71_603_pixels.png

That is the text as-is found in the "name" key of the json response. There were others, but this one is the one that I bothered to print out and capture the entire response.json()['files'].

I have very little python experience, Next to none. So if you or anyone reading into this know something I can do to iron out these encoding issues, it would be a great help.

Thanks in advance for any help on this, and thanks for making this bit of awesome goodnes available to the public.

UPDATE:
Greetings, I ended up modifying this gist to deal with utf-8 encoding. This is because I had filenames that came with special characters in the name, things like: á ñ ó and so on...

I'm also importing these:

import sys
import codecs

After imports I add:

sys.stdout=codecs.getwriter("utf-8")(sys.stdout, 'strict')

Then in the line that prints out: print "Deleting file " + f["name"] + "..." add .decode('utf-8') at the end.

That took care of the encoding issues I was having.

Also, after reading up on python syntax, I was able to modify this gist into one that would loop over a list of access tokens in order to delete files from more than one user.

Ended up looking like this: https://gist.github.com/JLMorales/53fcf1f483f357e81d1f

@stucka
Copy link

stucka commented Jun 9, 2016

I'd seen this code posted elsewhere, not in the gist, so I started reworking it without realizing I could do a pull request ... then searched for a line in the code again and found this gist.

Anyway, a partial reworking with utf-8 handling, improved messaging and multiple accounts is posted here:
https://github.com/PalmBeachPost/SlackPruner

Thanks! I hope this is OK. If you'd rather fold some of this back into the gist or something, please let me know.

@theapplepastor
Copy link

Im getting an error running this

File "slack2.py", line 20
print "Deleting file " + f["name"] + "..."
^
SyntaxError: Missing parentheses in call to 'print'

@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