delete-user.mdx
April 16, 2026 ยท View on GitHub
<If notSdk={["nextjs", "astro", "expressjs", "fastify", "nuxt", "react-router", "tanstack-react-start"]}>
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 })
}
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 })
}
</If>
<If sdk="astro">
```tsx {{ filename: 'src/api/example.ts' }}
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 })
}
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 })
})
</If>
<If sdk="fastify">
```ts {{ filename: 'src/routes/example.ts' }}
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 })
})
}
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 }
})
</If>
<If sdk="react-router">
```tsx {{ filename: 'app/routes/example.tsx' }}
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 })
}
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 })
},
},
},
})
</If>