the-littlest-jupyterhub icon indicating copy to clipboard operation
the-littlest-jupyterhub copied to clipboard

Deleting accounts from the command line

Open JuanCab opened this issue 5 years ago • 5 comments

When I remove "allowed users" using tljh-config, I noticed those users are still listed in the JupyterHub admin interface. I assume they can't log in, but is there any way to remove the users from whatever database JupyterHub is using for its internal list of users? I ask because we have to add/delete users at the end of every semester and I would really like to automate this part of the process.

JuanCab avatar May 28 '19 22:05 JuanCab

@JuanCab I'm also interested in this. Have you found any workarounds or solutions?

mdpiper avatar May 14 '20 17:05 mdpiper

None found yet. I ended up switching away from TLJH instead. :(

JuanCab avatar May 14 '20 17:05 JuanCab

@JuanCab Ouch. OK--thanks for this info!

mdpiper avatar May 14 '20 17:05 mdpiper

Just deleted several hundred users with the following method:

  1. Request an API token
  2. Create a notebook on your server with the following to get a list of users
import requests

token = 'your-magic-token'
api_url = 'https://your.domain.com/hub/api'

r = requests.get(api_url + '/users',
    headers={
             'Authorization': 'token %s' % token,
            "Content-Type": "application/json",
            }
    )

r.raise_for_status()
users = r.json()
  1. Delete users. Code below assumes a matching name prefix, but this could be replaced with a given list of names
matching_users = [u['name'] for u in users if u['name'].startswith('e1')]
for u in matching_users:
    requests.delete(api_url + "/users/" + u, 
         headers={
             'Authorization': 'token %s' % token,
            "Content-Type": "application/json",
            })
  1. Check in your admin console that the users are gone. You may need to reload the page.

This isn't quite as easy as the command line, but it's not too bad.

aolney avatar Aug 29 '21 00:08 aolney

I wrote a bash script that completely removes a user from the command line. You can find it here: https://github.com/JobinJohan/jupyterhub-utility-scripts.

More precisely, when deleting a user, the delete-user-from-system.sh script deletes:

  • All records related to the user in the jupyterhub.sqlite database.
  • The linux account of the user (showed in /etc/passwd).
  • The /home folder of the user and all its content.

Hope it helps 📈😀

JobinJohan avatar Aug 08 '22 21:08 JobinJohan