Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Last active July 27, 2024 22:20
Show Gist options
  • Save DexterHaslem/0ec2cb685569e813e11884c558f673cc to your computer and use it in GitHub Desktop.
Save DexterHaslem/0ec2cb685569e813e11884c558f673cc to your computer and use it in GitHub Desktop.
delete empty old rooms from matrix synapse
#!/bin/bash
# cleanup empty matrix synapse rooms -
# the matrix synapse admin experience is a total joke, barely above 'idk figure it out, ere are some apis'
# with that said this script will automatically purge all rooms with no users left in them which hang around in the
# db by default because that totally makes sense
# this is your synapse admin endpoint. in this example its localhost to avoid reverse proxy timeouts/etc
# this is mostly from calling the old v1 delete before which was synchronous and slow.
# beware the nginx etc reverse proxy settings in the docs DO NOT expose admin endpoint
# for nginx reverse proxies, you will want to change your location to eg:
# location ~ ^(/_matrix|/_synapse/client|\/_synapse\/admin) {.. (add |\/_synapse\/admin)
SADMIN="http://localhost:8008/_synapse/admin"
# this is your admin user auth token, you can get this in element by going to
# quick settings (bottom left gear) -> all settings -> help & about, scroll to bottom to reveal
AT="<your syt_... token here>"
# temp files to save room list and purge list
RF="rooms.json"
PF="purge.txt"
# if you have more than 500 rooms increase this count or run multiple times
# but be aware the deletes are queued async
curl --header "Authorization: Bearer $AT" "$SADMIN/v1/rooms?limit=500" > $RF
jq '.rooms[] | select(.joined_local_members == 0) | .room_id' < $RF > $PF
LINES=$(cat $PF)
for line in $LINES
do
rn="${line%\"}"
rn="${rn#\"}"
URL="$SADMIN/v2/rooms/$rn"
echo "deleting room $rn ($URL)"
# beware: body is required even if empty object.. for very good reasons im sure
curl --header "Authorization: Bearer $AT" -X DELETE -H "Content-Type: application/json" -d "{}" "$URL"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment