[PR #4838] [CLOSED] feat: add delete hooks to database operations #5606

Closed
opened 2026-03-13 12:29:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4838
Author: @naaa760
Created: 9/23/2025
Status: Closed

Base: canaryHead: feat/database-hooks-delete-clean


📝 Commits (2)

  • 6deb568 feat: add delete hooks to database operations
  • b27cd7b Merge branch 'canary' into feat/database-hooks-delete-clean

📊 Changes

3 files changed (+182 additions, -37 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.ts (+37 -37)
📝 packages/better-auth/src/db/with-hooks.ts (+77 -0)
📝 packages/better-auth/src/types/options.ts (+68 -0)

📄 Description

Summary

  • Adds delete hooks support to Better Auth database operations, enabling plugins and applications to intercept and customize delete operations.

#4811

Changes

  • Database Hooks: Added delete hooks to BetterAuthOptions["databaseHooks"] for user, session, account, and verification models
  • Hook Implementation: Created deleteWithHooks and deleteManyWithHooks functions in with-hooks.ts
  • Internal Adapter: Updated all delete operations in internal-adapter.ts to use the new hook system

Features

  • Before Hooks: Execute custom logic before deletion (can prevent deletion by returning false)
  • After Hooks: Execute cleanup logic after successful deletion
  • Consistent API: Same pattern as existing create/update hooks
  • Type Safety: Full TypeScript support with proper typing

Use Cases

  • Plugin cleanup when users are deleted
  • Audit logging for delete operations
  • Data validation before deletion
  • External system synchronization

Breaking Changes

  • None - this is purely additive functionality.

Summary by cubic

Adds delete hooks to database operations for user, session, account, and verification models so plugins can run logic before/after deletes and optionally block them. All delete paths in the internal adapter now go through the hook system.

  • New Features
    • Added delete.before and delete.after hooks in BetterAuthOptions for user, session, account, and verification.
    • Implemented deleteWithHooks and deleteManyWithHooks helpers.
    • Before hooks can cancel deletion; after hooks run post-delete cleanup. Fully typed and consistent with existing create/update hooks.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4838 **Author:** [@naaa760](https://github.com/naaa760) **Created:** 9/23/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/database-hooks-delete-clean` --- ### 📝 Commits (2) - [`6deb568`](https://github.com/better-auth/better-auth/commit/6deb568499f6fcca9050796b56408ab3ac0784c9) feat: add delete hooks to database operations - [`b27cd7b`](https://github.com/better-auth/better-auth/commit/b27cd7b1d2590b578ba0a864d36bd8c647edf220) Merge branch 'canary' into feat/database-hooks-delete-clean ### 📊 Changes **3 files changed** (+182 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.ts` (+37 -37) 📝 `packages/better-auth/src/db/with-hooks.ts` (+77 -0) 📝 `packages/better-auth/src/types/options.ts` (+68 -0) </details> ### 📄 Description ### Summary - Adds delete hooks support to Better Auth database operations, enabling plugins and applications to intercept and customize delete operations. #4811 ## Changes - **Database Hooks**: Added `delete` hooks to `BetterAuthOptions["databaseHooks"]` for user, session, account, and verification models - **Hook Implementation**: Created `deleteWithHooks` and `deleteManyWithHooks` functions in `with-hooks.ts` - **Internal Adapter**: Updated all delete operations in `internal-adapter.ts` to use the new hook system ### Features - **Before Hooks**: Execute custom logic before deletion (can prevent deletion by returning `false`) - **After Hooks**: Execute cleanup logic after successful deletion - **Consistent API**: Same pattern as existing create/update hooks - **Type Safety**: Full TypeScript support with proper typing ### Use Cases - Plugin cleanup when users are deleted - Audit logging for delete operations - Data validation before deletion - External system synchronization ### Breaking Changes - None - this is purely additive functionality. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds delete hooks to database operations for user, session, account, and verification models so plugins can run logic before/after deletes and optionally block them. All delete paths in the internal adapter now go through the hook system. - **New Features** - Added delete.before and delete.after hooks in BetterAuthOptions for user, session, account, and verification. - Implemented deleteWithHooks and deleteManyWithHooks helpers. - Before hooks can cancel deletion; after hooks run post-delete cleanup. Fully typed and consistent with existing create/update hooks. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:29:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5606