Deleting users
Clerk currently supports deleting users through our backend API(opens in a new tab).
The deleteUser()
method takes a single argument: the user ID of the user you want to delete. It can be accessed from the users
sub-api of the clerkClient
instance.
app/private/route.tsimport { clerkClient } from "@clerk/nextjs"; import { NextResponse } from 'next/server'; export async function DELETE(request: Request) { const { userId } = await request.body.json(); try { await clerkClient.users.deleteUser(userId); return NextResponse.json({ message: 'Success' }); } catch (error) { console.log(error); return NextResponse.json({ error: 'Error deleting user' }); } }
pages/api/private.tsimport { clerkClient } from "@clerk/nextjs/server"; import { NextApiRequest, NextApiResponse } from "next"; export default async function handler(req: NextApiRequest, res: NextApiResponse) { const userId = req.body.userId; try { await clerkClient.users.deleteUser(userId); return res.status(200).json({ message: 'Success' }); } catch (error) { console.log(error); return res.status(500).json({ error: 'Error deleting user' }); } }
private.tsimport { clerkClient } from "@clerk/clerk-sdk-node"; app.post('/deleteUser', (req, res) => { const userId = req.body.userId; try { await clerkClient.users.deleteUser(userId); return res.status(200).json({ message: 'Success' }); } catch (error) { console.log(error); return res.status(500).json({ error: 'Error deleting user' }); } });
curl.shcurl -XDELETE -H 'Authorization: CLERK_SECRET_KEY' 'https://api.clerk.com/v1/users/{user_id}'
Last updated on November 21, 2023