How to delete user programatically?


I’m trying to delete Netlify user programatically but can’t seem to find any docs about it.

How can I achieve this?

I’ve created a function based on

It doesn’t succeed as the context I get is just an empty object: {"clientContext":{}}.

I see the context gets identity and user. How do I pass them using context from the client?

EDIT: I’ve passed the data using request body instead. However, one issue is that function failed because the token was expired. Second issue is that I’m getting code: 401, msg: 'User not allowed' when trying to delete the user. How can I resolve these issues?

The tokens we issue last only an hour, so you’d need to request one with every invocation before using it, I think - you couldn’t hardcode anything. Are you saying it never works, or that it only works sometimes?

Unless deleting gotrue user from the browser storage and logging back in isn’t enough to get a new token, I can never get to delete the user. It always returns a 401:

Deleted a user!
{ data: { code: 401, msg: 'User not allowed' } }
Response with status 204 in 414 ms.

On a related note, how do I request a new token?

Hi @abtx, you can’t delete a user from the browser because the Identity token users get doesn’t have admin access to your Identity instance. In Netlify functions there is a short lived Identity token that you can use to perform admin actions like deleting a user. If you already wrote a function, can you share the source code for it so we can take a look?