Overview
Concrete tool for pruning old telemetry events from D1 to reclaim space, with scheduled and emergency modes, provided by the telemetry worker.
Description
The pruneOldEvents function runs on a nightly cron schedule to delete high-volume event rows older than retention windows. The emergencyPrune function triggers when D1 size exceeds the soft limit (8 GB): retention windows are halved and rows are deleted with a rate-limit per isolate to avoid overwhelming D1. Both functions also clean up oauth_accounts entries. Emergency pruning has a cooldown to prevent repeated triggers.
Usage
- Scheduled prune: Runs nightly via Cloudflare Workers cron trigger.
- Emergency prune: Triggered when
observeDbSize detects D1 size over the soft limit.
Code Reference
Source Location
- Repository: jcode
- File: telemetry-worker/src/worker.js
Signature
async function pruneOldEvents(env, options?)
async function emergencyPrune(env)
// Constants:
const D1_SOFT_LIMIT_BYTES = 8_000_000_000;
const EMERGENCY_PRUNE_COOLDOWN_MS = /* ... */;
Import
// Internal worker functions, called from the scheduled handler.
// telemetry-worker/src/worker.js
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| env |
Env |
Yes /// Cloudflare Worker environment with D1 binding
|
| options |
object |
No /// Prune options (retention windows, batch size)
|
Outputs
| Name |
Type |
Description
|
| D1 rows |
deleted /// High-volume event rows and oauth_accounts cleaned
|
| free pages |
reclaimed /// D1 free-page pool recycled
|
Usage Examples
Scheduled Handler
export default {
async scheduled(event, env, ctx) {
// Nightly cron: prune old events
ctx.waitUntil(pruneOldEvents(env));
},
};
Related Pages
Implements Principle
Requires Environment
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.