[PR #5114] [MERGED] fix(db): Refactor account deletion functions to use database hooks #14033

Closed
opened 2026-04-13 09:16:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5114
Author: @xuchenhao001
Created: 10/6/2025
Status: Merged
Merged: 10/6/2025
Merged by: @Bekacru

Base: canaryHead: fix/delete-accounts-uses-database-hooks


📝 Commits (1)

  • 3de5423 fix(db): ensure account deletion uses database hooks

📊 Changes

2 files changed (+89 additions, -15 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.test.ts (+69 -0)
📝 packages/better-auth/src/db/internal-adapter.ts (+20 -15)

📄 Description

Description:

When using better-auth (specifically version v1.4.0-beta.6), the databaseHooks.account.delete.after hook is not triggered when an account is unlinked via the /unlink-account endpoint, despite the underlying account record being successfully deleted from the database.

Steps to Reproduce:

  1. Configure better-auth with a databaseHooks.account.delete.after hook in auth.ts (similar to the provided example in the initial problem description).
  2. Link an account to a user. Observe that databaseHooks.account.create.after is successfully triggered.
  3. Unlink the previously linked account.
  4. Verify that the account record is indeed deleted from the database.
  5. Observe that databaseHooks.account.delete.after is not triggered, and no associated logs are produced.

Expected Behavior:

The databaseHooks.account.delete.after hook should be triggered after an account is successfully deleted from the database by the unlinkAccount endpoint.

Actual Behavior:

The databaseHooks.account.delete.after hook is not triggered when an account is unlinked, even though the account record is removed from the database.

Root Cause Analysis:

Upon reviewing the source code, specifically packages/better-auth/src/db/internal-adapter.ts, it was found that the internalAdapter.deleteAccount function (which is called by the /unlink-account endpoint) directly invokes the underlying database adapter's delete method. It bypasses the deleteWithHooks wrapper, which is responsible for dispatching the before and after database hooks.

This change will ensure that the deleteAccount operation correctly dispatches the databaseHooks.account.delete.after hook.

Testing

New unit tests have been added and verified locally. All existing tests are expected to pass.

Checklist

  • Code is type-safe and takes full advantage of TypeScript features.
  • Code follows existing style and conventions.
  • Unit tests have been added for new/modified functionality.
  • Code has been formatted with pnpm format and linted with pnpm lint:fix (User will handle manually).
  • Commit message follows the specified format.
  • PR description clearly outlines changes, rationale, and testing.

Summary by cubic

Refactored account deletion to use database hooks so unlinking an account triggers databaseHooks.account.delete.after.

  • Bug Fixes
    • deleteAccount and deleteAccounts now call deleteWithHooks/deleteManyWithHooks instead of direct adapter deletes.
    • Added tests for deleting a single account and all accounts for a user.

🔄 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/5114 **Author:** [@xuchenhao001](https://github.com/xuchenhao001) **Created:** 10/6/2025 **Status:** ✅ Merged **Merged:** 10/6/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/delete-accounts-uses-database-hooks` --- ### 📝 Commits (1) - [`3de5423`](https://github.com/better-auth/better-auth/commit/3de542399863841c2a89b65f356bb8c2cc9cf4f1) fix(db): ensure account deletion uses database hooks ### 📊 Changes **2 files changed** (+89 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.test.ts` (+69 -0) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+20 -15) </details> ### 📄 Description **Description:** When using `better-auth` (specifically version `v1.4.0-beta.6`), the `databaseHooks.account.delete.after` hook is not triggered when an account is unlinked via the `/unlink-account` endpoint, despite the underlying account record being successfully deleted from the database. **Steps to Reproduce:** 1. Configure `better-auth` with a `databaseHooks.account.delete.after` hook in `auth.ts` (similar to the provided example in the initial problem description). 2. Link an account to a user. Observe that `databaseHooks.account.create.after` is successfully triggered. 3. Unlink the previously linked account. 4. Verify that the account record is indeed deleted from the database. 5. Observe that `databaseHooks.account.delete.after` is *not* triggered, and no associated logs are produced. **Expected Behavior:** The `databaseHooks.account.delete.after` hook should be triggered after an account is successfully deleted from the database by the `unlinkAccount` endpoint. **Actual Behavior:** The `databaseHooks.account.delete.after` hook is not triggered when an account is unlinked, even though the account record is removed from the database. **Root Cause Analysis:** Upon reviewing the source code, specifically `packages/better-auth/src/db/internal-adapter.ts`, it was found that the `internalAdapter.deleteAccount` function (which is called by the `/unlink-account` endpoint) directly invokes the underlying database adapter's `delete` method. It bypasses the `deleteWithHooks` wrapper, which is responsible for dispatching the `before` and `after` database hooks. This change will ensure that the `deleteAccount` operation correctly dispatches the `databaseHooks.account.delete.after` hook. ## Testing New unit tests have been added and verified locally. All existing tests are expected to pass. ## Checklist - [x] Code is type-safe and takes full advantage of TypeScript features. - [x] Code follows existing style and conventions. - [x] Unit tests have been added for new/modified functionality. - [x] Code has been formatted with `pnpm format` and linted with `pnpm lint:fix` (User will handle manually). - [x] Commit message follows the specified format. - [x] PR description clearly outlines changes, rationale, and testing. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Refactored account deletion to use database hooks so unlinking an account triggers databaseHooks.account.delete.after. - **Bug Fixes** - deleteAccount and deleteAccounts now call deleteWithHooks/deleteManyWithHooks instead of direct adapter deletes. - Added tests for deleting a single account and all accounts for a user. <!-- 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-04-13 09:16:38 -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#14033