Skip to content

Instantly share code, notes, and snippets.

@herrri
Last active April 3, 2020 11:02
Show Gist options
  • Save herrri/6d3beebcfc7ac87f19498eaa2c2bdacf to your computer and use it in GitHub Desktop.
Save herrri/6d3beebcfc7ac87f19498eaa2c2bdacf to your computer and use it in GitHub Desktop.
How to remove users form auth0 using python API
#
# pip install auth0-python python-dotenv
# also configure your auth0 and set the .env
#
import os
import sys
import time
from auth0.v3.authentication import GetToken
from auth0.v3.management import Auth0, Users
from dotenv import load_dotenv
load_dotenv()
domain = os.getenv("DOMAIN")
non_interactive_client_id = os.getenv("CLIENT_ID")
non_interactive_client_secret = os.getenv("CLIENT_SECRET")
def throttle(func):
"""
Throttles the requests
2 req / sec is max
"""
def wrapper(*args, **kwargs):
time.sleep(0.5)
return func(*args, **kwargs)
return wrapper
def get_mgmt_token():
"""
the auth0 management token
"""
get_token = GetToken(domain)
token = get_token.client_credentials(
non_interactive_client_id,
non_interactive_client_secret,
"https://{}/api/v2/".format(domain),
)
return token["access_token"]
@throttle
def _get_users(u, page=0):
"""
recursive paginated fetch
"""
res = u.list(page=page, per_page=100)
users = res["users"]
if len(users) < 100:
return users
return users + _get_users(u, page=(page + 1))
def main():
"""
main cli
"""
mgmt_api_token = get_mgmt_token()
auth0 = Auth0(domain, mgmt_api_token)
u = Users(domain, mgmt_api_token)
ids = [x["user_id"] for x in _get_users(u)]
print("Deleting...")
for idx, i in enumerate(ids):
@throttle
def delete(u, i):
u.delete(i)
delete(u, i)
sys.stdout.write(f"Progress {idx + 1}/{len(ids)} -- {i}")
if idx + 1 != len(ids):
sys.stdout.write("\r")
else:
sys.stdout.write("\n")
sys.stdout.flush()
print(f"Done.\nTotal removed users: {len(ids)}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment