Query icon
OutputValues icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Loop icon
Fail icon
Schedule icon

SurrealDB Dangling Record Link Cleanup Guard

Catch SurrealDB field record links pointing at deleted ids, alert Slack, then clear them to NONE behind an approval and re-verify.

Categories
DataInfrastructure

A record link stored in a normal SurrealDB field is a pointer with nothing enforcing it. UPDATE article:1 SET author = author:anna succeeds whether or not author:anna exists. DELETE author:anna afterwards does not touch the article. The field keeps the id of a record that is gone. The holding record still looks healthy, so SELECT * FROM article shows nothing wrong. The breakage surfaces later: a reader gets NONE where it expected a record, a FETCH author returns nothing at all. Joins quietly lose rows.

This is not the graph-edge case. An edge table built with RELATE is cascade-deleted when an endpoint goes, so those links clean themselves up. A link in an ordinary field is not, which is why it survives to be found later.

This blueprint tests each link against the target table's live ids. A link is dangling when the field holds a record id that is not in SELECT VALUE id FROM <target table>, which covers both a deleted target and a link written into the wrong table. It reports the exact ids, alerts Slack, then behind a dry-run gate and an approval clears the field to NONE one record at a time and re-reads the dangling set to prove the table is clean.

This blueprint was created by zkasuran.

Findings

Finding Rule Fix
Dead id the link is a record of the target table but that id is not live clear the field to NONE behind an approval, then re-read
Wrong table the link resolves to a record outside the target table same, with the real table named in the report so the write path can be traced
Empty field the field is NONE or absent None. type::is_record keeps it out of the result
Live link the id is in the target table's live ids None

Safety

  • Detection compares the link against the target table's live ids, not against whether the holding record exists. A row check passes every one of these records, because the record is always present. It is the pointer that is broken.
  • type::is_record keeps the test on real links, so a field that is already NONE or absent or holding a plain string is never reported and never touched.
  • Clearing the field to NONE is the conservative repair. It keeps the record, its history and every other field. It removes only the pointer that no longer resolves. This is the default.
  • Deleting the holding record is a different decision and it destroys data. Some teams do want it, for a join row that means nothing without its target. It is available as repair_action: delete_record behind a second input, confirm_delete_records. With that input left false a delete_record run refuses and fails rather than silently clearing the field instead, so the two repairs can never be confused.
  • A bare UPDATE article SET author = NONE would wipe the field on every row. This flow never issues one. Remediation loops over the id set from detection and each statement is UPDATE <one id> SET <field> = NONE WHERE <the dangling test>, so it names a single record and repeats the guard. A link repaired between detection and remediation is left alone, proven by a 0-row result on that id.
  • A repair needs auto_remediate true, dry_run false, a non-empty dangling set, then a human answering the Pause. For delete_record, confirm_delete_records as well.
  • Both loops are serial (concurrencyLimit: 1) because concurrent writes to one table race.
  • After the repair, reselect reads the dangling set again. If any link still points outside the target table the run fails, so a cleanup that did not land is never reported as success.
  • max_records caps how many records one run returns and repairs. A run that reaches the cap says so and asks to be re-run, rather than loading an unbounded result.

How it works

  1. detect (io.kestra.plugin.surrealdb.Query, fetchType: FETCH) selects the records whose link field holds an id outside SELECT VALUE id FROM <target table>, up to max_records. It returns each holding id as a canonical string plus the link and the table it points at. size is the dangling count.
  2. count_records reads the table total and how many records hold a link at all, so the report states the share that is broken.
  3. assess (io.kestra.plugin.core.output.OutputValues) derives the id set, the counts, the capped flag and the alert text once, so every branch reads the same values.
  4. report logs the summary. route (io.kestra.plugin.core.flow.If) stops on a clean table and continues on a finding.
  5. On a finding, alert_findings posts the dangling set to Slack and planned logs what a remediation run would change, on every run including a dry one.
  6. remediate_gate opens only with auto_remediate true, dry_run false and a non-empty set. hold (io.kestra.plugin.core.flow.Pause) waits for an answer.
  7. On approval, pick_action routes to remediate_clear (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1), which sets the field to NONE on each id with its guard repeated. The delete_record branch runs remediate_delete only when confirm_delete_records is true, otherwise refuse_delete fails the run.
  8. reselect reads the dangling set again and verify fails the run if any link still dangles. Without approval, declined records the decision and nothing changes.
  9. errors posts to Slack when the flow fails.

What you get

  • The exact ids of the records whose link field points at nothing, not a vague count.
  • Whether each one is a dead id in the right table or a link into the wrong table, which is the difference between a missed cascade and a bug in the write path.
  • A Slack alert the moment a scheduled run finds a dangling link.
  • A bounded repair that touches only the detected ids, each behind its own guard, with a re-read that fails the run when the change did not land.
  • Two repair choices with the destructive one gated separately, so the conservative fix is what you get by default.

Who it's for

  • Teams using SurrealDB record links in ordinary fields (author, owner, parent) where the target can be deleted by another code path.
  • Platform engineers who have seen a FETCH come back empty after a cleanup job or a restore.
  • Anyone who wants referential-integrity repair on a store that does not enforce it, with the destructive option kept out of reach.

Why orchestrate this with Kestra

SurrealDB has no scheduler, no approval step and no alerting of its own. Kestra runs the check on a cadence, holds the write half behind auto_remediate, dry_run and a Pause with the decision recorded on the execution, posts findings to Slack and keeps the read, the repair and the re-read in one auditable run. The id set from detection is carried straight into the repair loop, so remediation acts on what was found and nothing wider.

Tested end to end

Verified on Kestra 2.0.5 OSS against a SurrealDB 3.3.3 container on the same Docker network, with a local HTTP sink standing in for Slack. Every count below was read back from SurrealDB's own surreal sql, not only from the Kestra log. The article table started with three records: article:a1 holding author = author:anna, article:a2 holding the live author = author:bob, plus article:a3 with no author field at all. Deleting author:anna is what makes article:a1 dangle, with no statement ever touching article:a1 itself.

Run Execution Inputs Result
1 5J7lMmAn5J0zQxwHt3epC1 defaults, every link live 0 dangling of 2 linked, 3 records. clean logs, no Slack finding
2 3u3nhfyYzC7PQstZLRiM5F defaults, after DELETE author:anna 1 dangling of 2 linked, classified (dead id). Slack finding posted, planned logged. CLI after: still 1 dangling
3 750fcnnF86FQE3jf4Ud5tR auto_remediate: true, dry_run: true gate held, hold never ran, nothing changed. CLI after: 1 dangling
4 KYESGcyZmFA9JBSVKISFO auto_remediate: true, dry_run: false, resumed approved: true article:a1 cleared, reselect found 0, SUCCESS. CLI before 3 records with 1 dangling, after 3 records with 0 dangling, article:a2 still on author:bob with article:a3 untouched
5 39abEntAhJG1VQvY4pdabi the same, resumed approved: false declined, nothing changed. CLI after: 1 dangling
6 5xQ8D5lvEFClCpoAsryGeE repair_action: delete_record, confirm_delete_records: false, resumed approved: true refuse_delete failed the run, errors posted to Slack. CLI after: 3 records with 1 dangling, nothing deleted
7 AUwAWx452jdeDEILLUWD5 repair_action: delete_record, confirm_delete_records: true, resumed approved: true article:a1 deleted, reselect found 0, SUCCESS. CLI before 3 records with 1 dangling, after 2 records with 0 dangling, article:a2 plus article:a3 intact
8 6R3BqxPqYG3zFZ2fyc2K1D unreachable host detect failed, errors posted to Slack, FAILED
9 1BrlKreGck65yCjExfnNcE 2 dangling seeded (article:a1, article:a4), max_records: 1, resumed approved: true capped at 1, cleared article:a1, reselect found article:a4 still dangling, verify failed the run, errors posted. CLI before 2 dangling, after 1
10 vgq7JwyydYPLsTGS5ko8 article:a4 repointed at the live tag:x 1 dangling, classified (wrong table tag), so a link into a table that is not the target is caught as well as a dead id

Run 1 against run 2 isolates that the detector catches a real dangling link. Nothing changed between them except DELETE author:anna, which touched no article. The report moved from 0 findings to 1. Run 9 proves the re-verify can fail: one dangling link was cleared by exact id while the second was left outside the cap, so reselect still found one and the run failed rather than reporting a clean table. Runs 4 and 7 both left article:a2 (live link) with article:a3 (empty field) untouched, which is the precision claim. Run 6 proves the destructive action cannot be reached by setting repair_action alone. A guarded UPDATE article:a2 SET author = NONE WHERE <the dangling test> was also run directly in surreal sql and returned 0 rows, which is the per-statement guard refusing to clear a link that resolves.

Prerequisites

  • A SurrealDB instance reachable from the Kestra workers, with a user allowed to SELECT, UPDATE and, for the delete action, DELETE on the tables involved.
  • The plugin-surrealdb plugin, bundled with Kestra.
  • A table with a field holding record links, plus the table those links should resolve into.

Secrets

  • SLACK_WEBHOOK_URL: used by alert_findings and the errors block. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.
  • SURREALDB_PASSWORD: the database password, the default for the password input.

Inputs

Input Default Purpose
host surrealdb The SurrealDB host
namespace, database test, test Namespace and database to check
username, password root, secret Credentials
source_table article The table holding the link field
link_field author The field that holds a record link
target_table author The table whose live ids are the allowed set
max_records 1000 Ceiling on records returned and repaired per run
repair_action clear_link clear_link or delete_record
confirm_delete_records false Second gate, required for delete_record
approval_timeout PT2H How long the pause waits
auto_remediate false Allow the repair
dry_run true Must be false for a change to apply

The port (8000) and TLS (off) are set on the Query tasks. For a different port or a TLS endpoint, change port and useTls on each task.

Quick start

  1. Add the SLACK_WEBHOOK_URL and SURREALDB_PASSWORD secrets.
  2. Import the flow, set host, namespace, database, source_table, link_field and target_table, then run it. A clean table logs and stops. A dangling link sends a Slack alert and logs the id set.
  3. To repair, set auto_remediate: true and dry_run: false, run it, then answer the pause. The field is cleared to NONE on each detected id.
  4. Read the reselect step. The run fails if any link still points outside the target table.
  5. Enable the daily trigger, which always runs in safe mode.

How to extend

  • Guard several fields or tables by wrapping the tasks in a Loop over a list of table and field pairs. INFO FOR TABLE <name> reports the declared fields to drive it.
  • Repoint instead of clearing: replace the UPDATE with one that sets the field to a tombstone record, so readers can tell a repaired link from one that was never set.
  • Alert only above a threshold, so a handful of known dangling links stays quiet and a spike pages.
  • Add a SCHEMAFULL table definition with an assert on the field, which stops new dangling links being written while this flow cleans up the ones already stored.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.