Skip to main content
Docs

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.

Note

Using clerkClient varies based on the SDK you're using. Refer to the overview for usage details, including guidance on how to access the userId and other properties.

const userId = 'user_123'

const response = await clerkClient.users.deleteUser(userId)
example.js
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 })
}
app/api/example/route.ts
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 })
}
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 })
}
public.ts
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 })
})
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 })
  })
}
server/api/example.ts
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 }
})
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 })
}
app/routes/api/example.tsx
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.

terminal
  curl 'https://api.clerk.com/v1/users/{user_id}' -X DELETE -H 'Authorization:Bearer YOUR_SECRET_KEY' -H 'Content-Type:application/json'

Feedback

What did you think of this content?

Last updated on