Setting up Supabase Cron + Edge Functions is a powerful way to automate tasks like publishing, notifications, or queue processing. Here's a step-by-step guide tailored for your workflow:

⚙️ Step-by-Step: Setup Supabase Cron + Edge Function

1. Enable Required Extensions

Go to your Supabase dashboard:

  • Navigate to Database → Extensions

  • Enable:

    • pg_cron – for scheduling jobs

    • pg_net – for making HTTP requests to Edge Functions

    • vault – for securely storing secrets like anon_key and project_url

2. Create Your Edge Function

Use Supabase CLI:

bash
npx supabase functions new publish-worker

This creates a function at ./supabase/functions/publish-worker/index.ts. Example:

ts
export async function handler(req: Request): Promise<Response> {
  const payload = await req.json();
  console.log("Triggered at:", payload.time);
  // Add your publishing logic here
  return new Response("Publish task executed", { status: 200 });
}

Deploy it:

bash
npx supabase functions deploy publish-worker

3. Store Secrets in Vault

In SQL Editor:

sql
select vault.create_secret('https://your-project-ref.supabase.co', 'project_url');
select vault.create_secret('YOUR_SUPABASE_ANON_KEY', 'anon_key');

4. Schedule Cron Job

In SQL Editor:

sql
select cron.schedule(
  'publish-every-5-mins',
  '*/5 * * * *',  -- every 5 minutes
  $$
  select net.http_post(
    url := (select decrypted_secret from vault.decrypted_secrets where name = 'project_url') || '/functions/v1/publish-worker',
    headers := jsonb_build_object(
      'Content-type', 'application/json',
      'Authorization', 'Bearer ' || (select decrypted_secret from vault.decrypted_secrets where name = 'anon_key')
    ),
    body := jsonb_build_object('time', now())
  )
  $$
);

Option 1: Delete Cron Job via SQL

If you know the job name (e.g. "publish-every-5-mins"), run:sql

select cron.unschedule('publish-every-5-mins');
or 
select cron.unschedule(name) from cron.job where name like 'publish-%';