Skip to content

chore(coderd/database): add pruning for orphaned chat file_ids entries#23985

Draft
stirby wants to merge 1 commit intomainfrom
chore/prune-orphaned-chat-files
Draft

chore(coderd/database): add pruning for orphaned chat file_ids entries#23985
stirby wants to merge 1 commit intomainfrom
chore/prune-orphaned-chat-files

Conversation

@stirby
Copy link
Copy Markdown
Collaborator

@stirby stirby commented Apr 2, 2026

Summary

Add a dbpurge job that periodically deletes orphaned chat_files rows — files uploaded for chats but no longer referenced by any non-deleted chat message.

Changes

  • New SQL query DeleteOrphanedChatFiles in coderd/database/queries/chatfiles.sql: deletes chat_files rows older than 24 hours that are not referenced by any non-deleted chat_messages content (JSONB file_id field).
  • dbpurge integration: registered in the existing purgeTick loop with a 24-hour age threshold, matching the pattern of other purge jobs (logging, metrics).
  • Authorization: uses rbac.ResourceSystem / policy.ActionDelete in dbauthz, consistent with other bulk-delete operations.
  • Test: TestDeleteOrphanedChatFiles validates three scenarios: orphaned old file (deleted), referenced old file (kept), recent orphaned file (kept).

Closes #23910

Add a new DeleteOrphanedChatFiles SQL query that removes chat_files rows
older than 24 hours that are not referenced by any non-deleted chat
message's content. Register this as a periodic job in the dbpurge
package, following the existing pattern used by other purge operations.

The query scans the JSONB content array of chat_messages for file_id
references and preserves any chat_files still in use.
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.

chore: add pruning for orphaned chat file_ids entries

1 participant