Skip to content

fix(media): add periodic cleanup for all media files#24519

Closed
heyhudson wants to merge 1 commit intoopenclaw:mainfrom
heyhudson:fix/inbound-media-cleanup
Closed

fix(media): add periodic cleanup for all media files#24519
heyhudson wants to merge 1 commit intoopenclaw:mainfrom
heyhudson:fix/inbound-media-cleanup

Conversation

@heyhudson
Copy link
Copy Markdown
Contributor

@heyhudson heyhudson commented Feb 23, 2026

Summary

Add a gateway-lifecycle media cleanup timer that periodically removes old files from all ~/.openclaw/media/ subdirectories based on a configurable TTL. Default retention window is 24 hours, configurable via media.ttlHours.

What Changed

  • src/gateway/server-maintenance.ts: startGatewayMaintenanceTimers() now schedules media cleanup hourly and runs one startup pass.
  • src/gateway/server.impl.ts: Computes the cleanup TTL from config.media.ttlHours (default: 24 hours) and passes it to maintenance timers.
  • src/gateway/server-close.ts: Clears the new media cleanup interval during shutdown.
  • src/media/store.ts: Whitespace-only change (blank line added).
  • src/config/types.openclaw.ts: Added media.ttlHours config type.
  • src/config/zod-schema.ts: Added Zod schema for media.ttlHours.
  • src/config/schema.help.ts: Added help text for media.ttlHours.
  • src/config/schema.labels.ts: Added label for media.ttlHours.

Testing

  • src/gateway/server-maintenance.test.ts (new): Verifies the media cleanup timer calls cleanOldMedia(ttlMs) on schedule.
  • src/media/store.test.ts: Verifies cleanup removes old files across media subdirectories.

AI Disclosure

  • AI-assisted
  • Fully tested

@heyhudson heyhudson force-pushed the fix/inbound-media-cleanup branch 2 times, most recently from 46e277b to 4f6dfa9 Compare February 23, 2026 15:51
@heyhudson heyhudson changed the title fix(media): add periodic cleanup for inbound media files fix(media): add periodic cleanup for all media files Feb 23, 2026
@heyhudson heyhudson force-pushed the fix/inbound-media-cleanup branch 4 times, most recently from 16f1e96 to c63cc62 Compare March 2, 2026 13:53
@heyhudson heyhudson closed this Mar 2, 2026
@heyhudson heyhudson reopened this Mar 2, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 2, 2026

Greptile Summary

Added periodic media cleanup timer that runs hourly to remove old files from ~/.openclaw/media/ based on a configurable TTL (default: 24 hours via media.ttlHours).

Key changes:

  • cleanOldMedia() in src/media/store.ts walks the media directory and first-level subdirectories, removing files older than the configured TTL based on modification time
  • Gateway maintenance timers schedule cleanup to run hourly plus once on startup
  • Configuration properly validated via Zod schema (non-negative numbers only)
  • Cleanup interval properly cleared during gateway shutdown
  • Comprehensive test coverage for both the cleanup logic and timer scheduling

Implementation notes:

  • The cleanup uses mtime for age calculation, which is appropriate for this use case
  • Error handling uses silent catches for individual file operations while logging errors at the top level
  • The Math.max(0, mediaTtlHours) provides defense-in-depth even though Zod already validates non-negative values
  • Cleanup runs every hour (60 * 60_000 ms), which is reasonable for the default 24-hour retention window

Confidence Score: 5/5

  • This PR is safe to merge with no identified issues
  • The implementation is clean and well-tested. Configuration validation is thorough with Zod schema, edge cases are handled correctly (Infinity, NaN, negative values), cleanup logic is sound and matches current usage patterns, timer lifecycle management is complete with proper shutdown cleanup, and test coverage is comprehensive covering both unit tests for cleanup logic and integration tests for timer scheduling.
  • No files require special attention

Last reviewed commit: c63cc62

@vincentkoc
Copy link
Copy Markdown
Member

Picked this up in #38292 so the final media fix can land as one scoped PR.

That branch carries forward the top-level media.ttlHours config and gateway-owned cleanup timer from this PR, then folds in the nested-directory coverage from #29211 so issue #33078 is handled end to end.

Keeping this linked for contributor credit because the retention-config and gateway-lifecycle pieces came from here.

@vincentkoc
Copy link
Copy Markdown
Member

Resolved in #38292

@vincentkoc vincentkoc closed this Mar 7, 2026
@heyhudson heyhudson deleted the fix/inbound-media-cleanup branch March 7, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants