[PR #9150] feat(api-key): add option to disable inline expired key cleanup #16715

Open
opened 2026-04-13 10:39:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/9150
Author: @GautamBytes
Created: 4/13/2026
Status: 🔄 Open

Base: mainHead: feat/disable-inline-cleanup


📝 Commits (3)

  • 62ffe9d feat(api-key): add option to disable inline expired key cleanup
  • dc53103 fix(api-key): address review feedback
  • d4aa32a fix(api-key): preserve deferred verify cleanup

📊 Changes

12 files changed (+430 additions, -29 deletions)

View changed files

.changeset/rare-rocks-rest.md (+5 -0)
📝 docs/content/docs/plugins/api-key/index.mdx (+4 -0)
📝 docs/content/docs/plugins/api-key/reference.mdx (+8 -1)
📝 packages/api-key/src/api-key.test.ts (+343 -0)
📝 packages/api-key/src/index.ts (+24 -11)
📝 packages/api-key/src/routes/create-api-key.ts (+3 -1)
📝 packages/api-key/src/routes/delete-api-key.ts (+3 -1)
📝 packages/api-key/src/routes/get-api-key.ts (+3 -1)
📝 packages/api-key/src/routes/list-api-keys.ts (+18 -9)
📝 packages/api-key/src/routes/update-api-key.ts (+3 -1)
📝 packages/api-key/src/routes/verify-api-key.ts (+7 -4)
📝 packages/api-key/src/types.ts (+9 -0)

📄 Description

Description

This PR introduces a configuration option to disable the automatic, inline cleanup of expired API keys.

Currently, the api-key plugin runs deleteAllExpiredApiKeys() on every route handler (create, verify, list, update, delete). For applications that generate high volumes of short-lived tokens, this causes significant database performance bottlenecks (full table scans, race conditions bypassing the in-memory throttle, and cascading deletes).

By adding keyExpiration.autoCleanup (defaulting to true for backward compatibility), developers can opt out of this inline overhead and manage cleanup externally (e.g., via a background worker or cron job hitting the manual endpoint).

Closes #9141

Changes Made

  • Added autoCleanup?: boolean to ApiKeyConfigurationOptions under the keyExpiration object.
  • Wrapped all inline deleteAllExpiredApiKeys() triggers in create, get, list, update, delete, and verify routes with the new flag.
  • Updated the session-mocking before hook to correctly evaluate the resolved configuration before triggering cleanup.
  • Ensured seamless integration with deferUpdates for serverless environments.
  • Added comprehensive unit/integration tests in api-key.test.ts to verify background tasks and standard endpoints respect the flag.
  • Documented the new option and external cron recommendation in index.mdx and reference.mdx.

Summary by cubic

Adds keyExpiration.autoCleanup to the api-key plugin so you can disable inline cleanup of expired keys. This reduces DB work on hot paths and lets you move cleanup to a cron or worker.

  • New Features
    • New keyExpiration.autoCleanup (default true) gates inline deleteAllExpiredApiKeys() in create, get, list, update, delete, and verify.
    • Respects per-config resolution across routes and session lookups; verify defers cleanup when enabled and deferUpdates=true, and won’t enqueue when autoCleanup=false (no inline cleanup when deferUpdates=false).
    • Docs updated and tests added; use auth.api.deleteAllExpiredApiKeys() for external cleanup.

Written for commit d4aa32ae1d. Summary will update on new commits.


🔄 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/9150 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 4/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/disable-inline-cleanup` --- ### 📝 Commits (3) - [`62ffe9d`](https://github.com/better-auth/better-auth/commit/62ffe9dcd7e9975da7d5f616e8c8e364cb02185b) feat(api-key): add option to disable inline expired key cleanup - [`dc53103`](https://github.com/better-auth/better-auth/commit/dc53103b8997f13278b9cf82fd582d32bc31474b) fix(api-key): address review feedback - [`d4aa32a`](https://github.com/better-auth/better-auth/commit/d4aa32ae1ded07ad787cf36ed05641daaa809c07) fix(api-key): preserve deferred verify cleanup ### 📊 Changes **12 files changed** (+430 additions, -29 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/rare-rocks-rest.md` (+5 -0) 📝 `docs/content/docs/plugins/api-key/index.mdx` (+4 -0) 📝 `docs/content/docs/plugins/api-key/reference.mdx` (+8 -1) 📝 `packages/api-key/src/api-key.test.ts` (+343 -0) 📝 `packages/api-key/src/index.ts` (+24 -11) 📝 `packages/api-key/src/routes/create-api-key.ts` (+3 -1) 📝 `packages/api-key/src/routes/delete-api-key.ts` (+3 -1) 📝 `packages/api-key/src/routes/get-api-key.ts` (+3 -1) 📝 `packages/api-key/src/routes/list-api-keys.ts` (+18 -9) 📝 `packages/api-key/src/routes/update-api-key.ts` (+3 -1) 📝 `packages/api-key/src/routes/verify-api-key.ts` (+7 -4) 📝 `packages/api-key/src/types.ts` (+9 -0) </details> ### 📄 Description ## Description This PR introduces a configuration option to disable the automatic, inline cleanup of expired API keys. Currently, the `api-key` plugin runs `deleteAllExpiredApiKeys()` on every route handler (`create`, `verify`, `list`, `update`, `delete`). For applications that generate high volumes of short-lived tokens, this causes significant database performance bottlenecks (full table scans, race conditions bypassing the in-memory throttle, and cascading deletes). By adding `keyExpiration.autoCleanup` (defaulting to `true` for backward compatibility), developers can opt out of this inline overhead and manage cleanup externally (e.g., via a background worker or cron job hitting the manual endpoint). Closes #9141 ## Changes Made - Added `autoCleanup?: boolean` to `ApiKeyConfigurationOptions` under the `keyExpiration` object. - Wrapped all inline `deleteAllExpiredApiKeys()` triggers in `create`, `get`, `list`, `update`, `delete`, and `verify` routes with the new flag. - Updated the session-mocking `before` hook to correctly evaluate the resolved configuration before triggering cleanup. - Ensured seamless integration with `deferUpdates` for serverless environments. - Added comprehensive unit/integration tests in `api-key.test.ts` to verify background tasks and standard endpoints respect the flag. - Documented the new option and external cron recommendation in `index.mdx` and `reference.mdx`. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds `keyExpiration.autoCleanup` to the `api-key` plugin so you can disable inline cleanup of expired keys. This reduces DB work on hot paths and lets you move cleanup to a cron or worker. - **New Features** - New `keyExpiration.autoCleanup` (default `true`) gates inline `deleteAllExpiredApiKeys()` in `create`, `get`, `list`, `update`, `delete`, and `verify`. - Respects per-config resolution across routes and session lookups; `verify` defers cleanup when enabled and `deferUpdates=true`, and won’t enqueue when `autoCleanup=false` (no inline cleanup when `deferUpdates=false`). - Docs updated and tests added; use `auth.api.deleteAllExpiredApiKeys()` for external cleanup. <sup>Written for commit d4aa32ae1ded07ad787cf36ed05641daaa809c07. Summary will update on new commits.</sup> <!-- 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 10:39:19 -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#16715