deleteUser()
Deletes a User given a valid ID.
function deleteUser(userId: string): Promise<User>- Name
userId- Type
string- Description
The ID of the user to delete.
const userId = 'user_123'
const response = await clerkClient.users.deleteUser(userId)import { createClerkClient } from '@clerk/backend'
// Initialize clerkClient
const clerkClient = createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY })
async function deleteUser(request) {
// Use the `request.auth` object to access `isAuthenticated` and the user's ID
const { isAuthenticated, userId } = request.auth
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
// Use the `deleteUser()` method to delete the user
await clerkClient.users.deleteUser(userId)
// Return the success status
return Response.json({ success: true })
}import { auth, clerkClient } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
export async function POST() {
const { isAuthenticated, userId } = await auth()
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return new NextResponse('Unauthorized', { status: 401 })
}
// Initialize clerkClient
const client = await clerkClient()
// Use the `deleteUser()` method to delete the user
await client.users.deleteUser(userId)
// Return the success status
return NextResponse.json({ success: true })
}import type { APIRoute } from 'astro'
import { clerkClient } from '@clerk/astro/server'
export const POST: APIRoute = async (context) => {
const { isAuthenticated, userId } = context.locals.auth()
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return new Response('Unauthorized', { status: 401 })
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient(context).users.deleteUser(userId)
// Return the success status
return new Response(JSON.stringify({ success: true }), { status: 200 })
}import { clerkClient, getAuth } from '@clerk/express'
app.post('/deleteUser', async (req, res) => {
const { isAuthenticated, userId } = getAuth(req)
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
res.status(401).json({ error: 'User not authenticated' })
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient.users.deleteUser(userId)
// Return the success status
res.status(200).json({ success: true })
})import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { clerkClient, getAuth } from '@clerk/fastify'
export const exampleRoutes = (fastify: FastifyInstance) => {
fastify.post('/deleteUser', async (req: FastifyRequest, res: FastifyReply) => {
const { isAuthenticated, userId } = getAuth(req)
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
res.status(401).json({ error: 'User not authenticated' })
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient.users.deleteUser(userId)
res.status(200).json({ success: true })
})
}import { clerkClient } from '@clerk/nuxt/server'
export default defineEventHandler(async (event) => {
const { isAuthenticated, userId } = event.context.auth()
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return createError({ statusCode: 401, statusMessage: 'User not authenticated' })
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient(event).users.deleteUser(userId)
return { success: true }
})import { clerkClient, getAuth } from '@clerk/react-router/server'
import type { Route } from './+types/example'
import { redirect } from 'react-router'
export async function action(args: Route.ActionArgs) {
const { isAuthenticated, userId } = await getAuth(args)
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return redirect('/sign-in?redirect_url=' + args.request.url)
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient(args).users.deleteUser(userId)
// Return the success status
return Response.json({ success: true })
}import { json } from '@tanstack/react-start'
import { createFileRoute } from '@tanstack/react-router'
import { auth, clerkClient } from '@clerk/tanstack-react-start/server'
export const ServerRoute = createFileRoute('/api/example')({
server: {
handlers: {
POST: async () => {
const { isAuthenticated, userId } = await auth()
// Protect the route by checking if the user is signed in
if (!isAuthenticated) {
return json({ error: 'Unauthorized' }, { status: 401 })
}
// Initialize clerkClient
// Use the `deleteUser()` method to delete the user
await clerkClient().users.deleteUser(userId)
return json({ success: true })
},
},
},
})Backend API (BAPI) endpoint
This method in the SDK is a wrapper around the BAPI endpoint DELETE/users/{user_id}. See the BAPI reference for more information.
Here's an example of making a request directly to the endpoint using cURL.
Replace YOUR_SECRET_KEY with your Clerk .
curl 'https://api.clerk.com/v1/users/{user_id}' -X DELETE -H 'Authorization:Bearer YOUR_SECRET_KEY' -H 'Content-Type:application/json'Feedback
Last updated on