Skip to content

Instantly share code, notes, and snippets.

@rgpublic
Last active August 7, 2022 00:20
Show Gist options
  • Save rgpublic/afdb4fc42804b15c644cef3bd172717a to your computer and use it in GitHub Desktop.
Save rgpublic/afdb4fc42804b15c644cef3bd172717a to your computer and use it in GitHub Desktop.
#!/bin/bash
SERVER="matrix.mycompany.com"
echo -e "\e[97mEnter user you'd like to deactivate\e[0m";
read user
if [ "$user" == "" ]; then
echo "User required.";
exit;
fi
account="@$user:$SERVER";
su postgres -c "psql synapse -c 'DELETE FROM users WHERE name='\''$account'\'';'"
su postgres -c "psql synapse -c 'DELETE FROM user_directory WHERE user_id='\''$account'\'';'"
su postgres -c "psql synapse -c 'DELETE FROM account_data WHERE user_id='\''$account'\'';'"
su postgres -c "psql synapse -c 'DELETE FROM profiles WHERE user_id='\''$user'\'';'"
su postgres -c "psql synapse -c 'DELETE FROM user_external_ids WHERE user_id='\''$account'\'';'"
@rubo77
Copy link

rubo77 commented Aug 31, 2019

great!

please add this script as a Pull Request here in the folder https://github.com/matrix-org/synapse/tree/master/contrib/scripts

@Torxed
Copy link

Torxed commented Sep 2, 2019

This is the same workaround I proposed like a year+ ago.
This causes a lot of oddities, like "jump to the last read message" event keeps popping back up for some users, and not for others.

But this is the closest thing you'll get to a DELETE USER function..
Sadly, matrix is pretty broken fundamentally because of the focus of the UI in the last year.

@ordinarygulp
Copy link

I'm getting this on many of the commands, except the first one:
ERROR: column "name" does not exist

@rgpublic
Copy link
Author

The table structure seems to have changed :-(
user_directory and account_data now seem to use user_id. "profiles" has a column user_id. But it's not filled with user_ids :-( I don't know whether the table is still in use. You might need to delete the user from that table manually. I've modified the script a bit. Couldnt yet test it though. Gee, this is a mess.

@Programmierus
Copy link

Hello, what's the actual status of this script? Did you manage to amend it to match the current db structure?

@rgpublic
Copy link
Author

rgpublic commented Dec 2, 2020

I uncommented the last line back in. I compared it with my current database. It should still work with a recent synapse version. I haven't used the script for a while because (fortunately) no people were laid off :-) If anyone discovers additional tables that need to be included, please let me know. This won't delete any messages of those users I guess. You might want to disable the user first and perhaps use the expiration feature to expire old messages... Don't know whether messages properly expire after you removed the user from the database with this script, though.

Matrix is now used with Government agencies here in Germany and our beloved neighbor France AFAIK... I really, really wonder why no one ever brought this topic up before. I imagine this to be a real mess if you have thousands of users...

@disconn3ct
Copy link

There is an oidc mapping table also that will give headaches. If you miss user_external_ids you just get 'row not found' on login.

@rgpublic
Copy link
Author

rgpublic commented Jun 28, 2022

@disconn3ct : If I check that table on our database it is empty. But, alas, we don't have any external users anyway. Probably that's why. So what exactly do you mean by "will give headaches"? Are you proposing that I add the following to my script?

su postgres -c "psql synapse -c 'DELETE FROM user_external_ids WHERE user_id='\''$user'\'';'"

@disconn3ct
Copy link

Yes. Otherwise it still references the deleted user and when they attempt to log in again it says row not found. (If future readers just want to prevent login from SSO, disable already does that.)

@rgpublic
Copy link
Author

Thank you so much. I've changed it in the script. I cannot test it though. I don't know whether it should be the full account name ($account) or just the user name ($user). If you have any information on that, I'll be glad to know.

@disconn3ct
Copy link

It is user_id, which afaik is always @USER:SYNAPSE

@rgpublic
Copy link
Author

Thanks again, @disconn3ct . I've changed the script again accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment