Skip to content

Logging: replace "Purge All Logs" with age-based delete control#735

Open
i-anubhav-anand wants to merge 2 commits into
WordPress:developfrom
i-anubhav-anand:enhance/log-cleanup-by-age
Open

Logging: replace "Purge All Logs" with age-based delete control#735
i-anubhav-anand wants to merge 2 commits into
WordPress:developfrom
i-anubhav-anand:enhance/log-cleanup-by-age

Conversation

@i-anubhav-anand

@i-anubhav-anand i-anubhav-anand commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

What?

Closes #689

Replaces the single "Purge All Logs" button in the Manage Logs panel with an age-based delete control: a dropdown (Older than 30 days / 90 days / 1 year / All logs) followed by a "Delete" button.

Why?

The existing "Purge All Logs" button is too blunt — it forces users to delete their entire log history even when they only want to remove old entries. This change gives users meaningful control over how much history to keep, while still allowing a full purge via the "All logs" option.

How?

PHP:

  • Added AI_Request_Log_Manager::delete_logs_older_than(int $days): int — delegates to the existing AI_Request_Log_Repository::cleanup_by_retention() for the batched SQL delete, or falls back to purge_all_logs() when $days === 0.
  • Extended the DELETE /ai/v1/logs REST endpoint with an optional before_days integer param (minimum: 0). When omitted or 0, all logs are purged (backwards-compatible). Negative values are rejected with a 400.

React:

  • Rewrote SettingsPanel to use a SelectControl (30 / 90 / 365 / 0 days) and a "Delete" button with a contextual confirmation message.
  • Updated handleDeleteLogs in index.tsx to accept days: number, pass it as before_days in the DELETE request, and call fetchLogs() after deletion so the table immediately reflects actual remaining entries.

Tests:

  • AI_Request_Log_ManagerTest: test_delete_logs_older_than_removes_only_old_entries, test_delete_logs_older_than_zero_purges_all
  • AI_Request_Log_ControllerTest: test_purge_logs_with_before_days_removes_only_old_entries, test_purge_logs_rejects_negative_before_days

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 4.6
Used for: Code scaffolding and test suggestions; all implementation details, logic, and tests were reviewed, tested locally, and edited by me.

Testing Instructions

  1. Enable the AI Request Logging experiment under AI → Settings → Experiments.
  2. Navigate to Tools → AI Request Logs.
  3. Insert logs at different ages (or use the REST API / WP-CLI to backdate entries).
  4. In the Manage Logs → Danger Zone panel, select an age threshold from the dropdown.
  5. Click Delete → confirm → verify only logs older than the selected threshold are removed and the table immediately updates without a page refresh.
  6. Select All logs and confirm → all entries are removed.
  7. Verify the Delete button is disabled when there are no logs.

Screenshots or screencast

Before After
Before - single Purge All Logs button After - age-based delete dropdown

Changelog Entry

Changed - Replace "Purge All Logs" button with an age-based delete control (30 days / 90 days / 1 year / All) in the Manage Logs panel.

Open WordPress Playground Preview

Closes WordPress#689.

Replaces the single "Purge All Logs" button in the AI Request Logs
settings panel with a SelectControl offering four options:
  - Older than 30 days
  - Older than 90 days
  - Older than 1 year
  - All logs

The two-step confirmation flow is preserved; the message adapts to the
selected threshold. Nothing is scheduled or persisted — every delete
action is manual and explicit.

On the PHP side, the existing DELETE /ai/v1/logs REST route now accepts
an optional before_days integer (default 0 = purge all). A new
delete_logs_older_than() manager method routes to the already-batched
cleanup_by_retention() when days > 0, or purge_all() when days = 0.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: i-anubhav-anand <anubhav24@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: itsgajendraSingh <gajendrasingh@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@dkotter dkotter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E2E tests and ESlint are failing on this PR. Those will need cleaned up before we proceed with a review here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a user-facing control for automatic log cleanup

2 participants