Skip to content

Instantly share code, notes, and snippets.

@faljse
Created December 20, 2017 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save faljse/f529fa3644208e5be9045ab91593fdec to your computer and use it in GitHub Desktop.
Save faljse/f529fa3644208e5be9045ab91593fdec to your computer and use it in GitHub Desktop.
Delete post attachements of deleted mattermost posts
#!/usr/bin/python3
#
# Execute via ctrontab:
# */10 * * * * mattermost /opt/mattermost/mmdel.py
import psycopg2
import os
datadir = "/opt/mattermost/data"
# Try to connect
try:
conn=psycopg2.connect(database="mattermost", user="mattermost", password="PASSWORD", host="localhost")
except Exception as e:
print ("I am unable to connect to the database." + str(e))
cur = conn.cursor()
try:
cur.execute("""SELECT path, posts.deleteat
FROM fileinfo
INNER JOIN posts ON fileinfo.postid=posts.id
WHERE posts.deleteat > 0;""")
except Exception as e:
print ("Select failed" + str(e))
rows = cur.fetchall()
for row in rows:
fileName = os.path.join(datadir, row[0])
if(os.path.isfile(fileName)):
print ("deleting ", fileName)
os.remove(fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment