Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup script for older messages
#!/bin/bash
# origin: https://github.com/matrix-org/synapse/issues/2964#issuecomment-376869097
logger "$0 started."
HOMEBASE="http://localhost"
ADMIN="@admin:matrix.example.org"
DBNAME="synapse"
TOKEN=$(sudo -u postgres psql -t -A --dbname="$DBNAME" --command="SELECT token FROM access_tokens WHERE user_id='$ADMIN' ORDER BY id DESC LIMIT 1;" 2>/dev/null)
TIME='30 days ago'
# # unix timestamp in milliseconds
UNIX_TIMESTAMP=$(date +%s%3N --date='TZ="UTC" '"$TIME")
ROOMS=$(sudo -u postgres psql -t -A --dbname="$DBNAME" --command="SELECT room_id FROM rooms;" 2>/dev/null)
echo "### MATRIX MAINTAINS"
echo "### purge history at $TIME:"
date --date='TZ="UTC" '"$TIME"
for ROOM_NAME in $ROOMS; do
echo "ROOM_ID: $ROOM_NAME"
curl --silent --header "Content-Type: application/json" --request POST \
--data '{"purge_up_to_ts":'$UNIX_TIMESTAMP',"delete_local_events": true}' \
$HOMEBASE':8008/_matrix/client/r0/admin/purge_history/'$ROOM_NAME'?access_token='$TOKEN
done
echo "### purge media cache:"
curl --silent --request POST $HOMEBASE':8008/_matrix/client/r0/admin/purge_media_cache?before_ts='$UNIX_TIMESTAMP'&access_token='$TOKEN
echo "### list rooms:"
sudo -u postgres psql -t -A --dbname="$DBNAME" --command="SELECT room_id, name FROM room_names;" 2>/dev/null
echo "### done."
logger "$0 stopped."
exit 0
# eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment