Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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