backend-auto-cleanup-worker

Builds a nightly Supabase edge function that purges expired cache, stale jobs, and orphaned storage.

Updated Sep 24, 2025
One-click install
npx skills add https://github.com/chriso789/pitch-1 --skill backend-auto-cleanup-worker-chriso789
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: backend-auto-cleanup-worker
Source: https://github.com/chriso789/pitch-1/tree/main/.agents/skills/backend-auto-cleanup-worker
Command: npx skills add https://github.com/chriso789/pitch-1 --skill backend-auto-cleanup-worker-chriso789

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Supabase backends accumulate expired function cache rows, failed webhook attempts, orphaned storage objects, stale AI jobs, and dead sessions that leak storage and degrade performance, and manual cleanup risks deleting active customer data. ## Core Features & Use Cases - Ten Mandatory Cleanup Tasks: Covers function_cache expiry, webhook attempt retention, temp uploads, abandoned draft estimates, orphaned storage, stale AI jobs, dead mobile sessions, log retention, duplicate imports, and empty contacts. - Safety-First Contract: Every task enforces active-data guards, dry-run-first deployment, batch limits, tenant isolation, and an audit row in cleanup_worker_runs. - Use Case: Schedule a pg_cron job at 03:15 that invokes the cleanup-worker edge function to purge 30-day-old failed webhook attempts and fail AI measurement jobs stuck in processing for over two hours, all logged with per-task scanned/deleted counts. ## Quick Start Ask the AI to build the cleanup-worker edge function with all ten cleanup tasks, dry-run enabled, and the pg_cron nightly schedule.

Frequently Asked Questions about backend-auto-cleanup-worker

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I schedule a nightly cleanup job in Supabase?▼

Use pg_cron to schedule a net.http_post call to the cleanup-worker edge function, for example at 03:15 nightly. The cron SQL includes the function URL and an Authorization header with an internal worker secret, never the service role key.

How to safely delete orphaned Supabase storage objects?▼

Delete the storage object first via storage.from(bucket).remove(paths), then delete the referencing database row. Verify the object's first path segment matches a known tenant_id to prevent cross-tenant deletion.

Can the cleanup worker delete active customer data?▼

No. Every delete query must exclude rows tied to active customers, jobs, leads, projects, or financial records via explicit JOIN or NOT EXISTS guards. Tables like tenants, users, invoices, and signed estimates are never touched.

What happens to AI jobs stuck in processing status?▼

Stale AI jobs are marked as failed, not deleted. The worker calls normalizeResultStateForWrite() to set result_state to ai_failed_unknown and records hard_fail_reason as cleanup_worker_timeout.

Why does the cleanup worker use dry-run mode first?▼

New tasks ship with dry_run=true so deletions are only counted and logged, not executed. Promotion to live deletes requires a separate change after at least one dry-run summary is stored in cleanup_worker_runs.

How are large cleanup batches handled without locking tables?▼

Deletes use DELETE ... WHERE id IN (SELECT id ... LIMIT N) with a default batch limit of 5000 rows. Long tasks like orphaned storage and old logs can split work across multiple nightly runs to stay under the 5-minute budget.