# Deleting an Organization

> How an owner deletes an organization with an emailed code, what's removed, and how to clean up your own data with after-delete hooks.

*Canonical: https://makerkit.dev/docs/void-cloudflare/organizations/deleting*

---

Only an owner can delete an organization, and only with a code emailed to them, so a stolen session alone can't. The organization, its members and invitations are removed for good; there's no undo.

This page is part of the [Organizations documentation](./overview).

## The Flow

1. Settings → Organization → **Danger zone** (owners only) → **Delete organization**.
2. **Email me a code**: the `sendDeletionCode` action checks the owner permission and the organization, then emails a 6-digit code ([Step-Up Codes](../authentication/step-up-codes)). Asking again within a minute (or past 5 an hour) answers with the wait; the code field stays open for a code already sent.
3. The owner types the code; the `deleteOrganization` action checks the permission and the organization again, verifies and consumes the code for **this** organization, then calls Better Auth's delete. Before the delete, the kit saves retryable database and storage purge jobs. If it cannot save them, the organization stays. With billing on, the organization's subscription is ended in Stripe first; if Stripe fails, nothing is deleted ([Cancellations](../billing/cancellations#deleting-an-organization)).
4. The owner lands on the app home: the personal context in `hybrid`, else their next organization or onboarding. Other members' sessions lose it on their next request.

A wrong, expired or used-up code gives one message under the field ("That code isn't right, or it's no longer valid") and deletes nothing. The server code is `organizations.delete` and `organizations.requestDeletionCode` in `@kit/organizations` (`packages/organizations/src/organizations.ts`), called through `organizationsFor(c)` (`apps/web/lib/organizations.server.ts`).

## After-Delete Hooks

Clean up your own data in `apps/web/config/organization-hooks.config.server.ts` (yours to edit):

```ts
import { db, eq } from 'void/db';

import type { DeletedOrganization } from '@kit/better-auth/server';

import { projectFiles } from '#/db/schema.ts';

export const afterOrganizationDelete: Array<
  (organization: DeletedOrganization) => Promise<void>
> = [
  async ({ id }) => {
    await db.delete(projectFiles).where(eq(projectFiles.organizationId, id));
  },
];
```

- Hooks run in order, after Better Auth has deleted the organization, with its `{ id, name }`.
- A hook that throws is logged (`organization after-delete hook failed`, with the organization's id) and the next one still runs: the delete has already happened. When the organization goes with its last member's [account deletion](../account/deletion), the same hooks run in a background job instead, where a throw fails the job and it retries from the first hook: write every hook to be safe to run twice.
- For rows that must never outlive their organization, prefer a foreign key with `onDelete: 'cascade'` ([Your Tables](./scoping-data#your-tables)): the database removes them in the same delete. Only for tables with few rows per organization.
- A hook runs in the request. Don't delete an unbounded table from it in one statement: declare a [purge target](../cleanup/purge-targets#recipe-purge-an-organizations-events). The kit requests it before the delete.

## Common Pitfalls

- A cascade or a single `DELETE` on a table with millions of rows per organization. The delete locks the table and stalls migrations and writes; make it a [purge target](../cleanup/purge-targets).

- Expecting a hook to stop the delete. It runs afterwards; nothing is rolled back.
- Deleting through `authClient.organization.delete` from the browser. The endpoint is closed (404); deletion is the page action, behind the code.

---

**Next:** [Organizations Overview →](./overview)
