DEV Community

Kazutora Hattori
Kazutora Hattori

Posted on

Automatically delete Supabase data every morning! I tried batch processing with GitHub Actions + ts-node

Introduction

When working with test user data in Supabase,
have you ever thought, "It's such a pain to manually delete old data every time..."

So this time, I used GitHub Actions and ts-node
to create a system that automatically deletes the previous day's user information every morning at 6:00 AM.

While creating this system, I learned how to execute Supabase operations from scripts, set up an automatic schedule (cron) for GitHub Actions, and securely manage keys using Secrets.

Lessons Learned

  1. Supabase is Easy to Use from Scripts

Connect with createClient() and easily manipulate data with .from('table').delete().

Supabase's JavaScript SDK is intuitive and works well with batch processing.

import { createClient } from '@supabase/supabase-js'

const supabase = createClient(process.env.URL!, process.env.KEY!)

await supabase
.from('users')
.delete()
.lt('created_at', `${yesterday}T23:59:59`)
Enter fullscreen mode Exit fullscreen mode
  1. Automatically execute jobs every morning at 6:00 AM using GitHub Actions

You can use on.schedule.cron to run jobs at a set time every day.
6:00 AM Japan time is set to 9:00 PM UTC.

on:
schedule:
- cron: "0 21 * * *" # JST 6:00
Enter fullscreen mode Exit fullscreen mode
  1. Securely handle keys with Secrets

Register your Supabase URL and API key in GitHub Secrets and safely use them as environment variables by passing them to env:.

env:
VITE_SUPABASE_URL: ${{ secrets.VITE_SUPABASE_URL }}
VITE_SUPABASE_ANON_KEY: ${{ secrets.VITE_SUPABASE_ANON_KEY }}
Enter fullscreen mode Exit fullscreen mode
  1. First Test Manually

By including workflow_dispatch, you can immediately run tests by clicking the "Run workflow" button without waiting for cron.

Summary

Supabase data deletion can be easily scripted using the SDK.

GitHub Actions can be used to automatically execute daily execution.

Secrets allow you to securely manage API keys.

Automation eliminates the time required for manual deletion and the risk of errors.

Top comments (0)