Skip to content

fix(api): delete snapshot build artifacts on snapshot deletion (stacked on #3797) - #3798

Open
AdaAibaby wants to merge 2 commits into
e2b-dev:mainfrom
AdaAibaby:fix/snapshot-artifact-cleanup
Open

AdaAibaby wants to merge 2 commits into
e2b-dev:mainfrom
AdaAibaby:fix/snapshot-artifact-cleanup

Conversation

@AdaAibaby

@AdaAibaby AdaAibaby commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Deletes the storage artifacts of a snapshot's exclusively-owned builds when the snapshot is deleted (DELETE /sandboxes/{id}), instead of orphaning them in GCS/S3.

A snapshot is backed by its own template, and deleteSnapshot already soft-deletes that template. This reuses the exact template-deletion cleanup for the snapshot's template, so the same guarantees apply.

Stacked on #3797. Review/merge #3797 first. This PR's own delta is one call site in sandbox_kill.go + a DB-backed exclusivity test.

Inherits the reachability guard

Because it reuses #3797's startTemplateArtifactCleanup, the snapshot path inherits the reachability guard, reconciliation metrics, and panic-safe/bounded/best-effort background behavior. The guard is what makes the base-deleted direction safe: if another live build reaches a base layer now DB-exclusive to this snapshot-template, it is kept, not orphaned. teamID is threaded through to scope the live-root set.

Test

DB-backed TestSnapshotArtifactExclusivity: the snapshot's own build and an extra exclusive build are returned for cleanup; a build shared with another template is excluded; the query resolves after soft-delete; every returned build carries the cluster_node_id needed to route DeleteBuild.


Update — rebased on #3797's checkpoint guard; snapshot-path test added

Rebased onto the updated #3797, so this PR inherits the checkpoint/snapshot live-root fix, the online grace window (TemplateArtifactMinAgeSeconds), and the pluggable fail-closed live-root sources.

Adds a DB-backed test on the snapshot path proving both halves of the checkpoint protection:

  1. deleting a snapshot never treats the base template's build as a deletion candidate (the base build is assigned to the base template, so GetExclusiveBuildsForTemplateDeletion for the snapshot's template excludes it), and
  2. the snapshot's own build is a live root when the base template is deleted, so the base build stays in the keep set instead of being orphaned.

Together with #3797's live-root source, deleting a checkpoint-derived snapshot removes only the snapshot's own COW diff layer and never the base it stacks on.

Stacked on #3797 (rebased onto its new tip). The handlers-package unit tests also require #3800 to run in CI; the DB-backed tests pass independently.

/cc @ryan-sana Looking forward to your code review.

…nciliation metrics (bounded, panic-safe)

Delete GCS/S3 build artifacts on template deletion, guarded by a header-reachability closure that protects still-referenced ancestor bases (checkpoint/snapshot), with an online grace window, fail-closed source union, and reconciliation metrics. Rebuilt onto latest main (db layer moved to packages/api/internal/db).
A snapshot is backed by its own template; on snapshot deletion, remove the build artifacts exclusively owned by that snapshot-template while protecting any base shared with a live template. Adds the exclusivity guard test (readmodel package, latest-main structure). Combines the two commits of e2b-dev#3798.
@AdaAibaby
AdaAibaby force-pushed the fix/snapshot-artifact-cleanup branch from 0e838b0 to 2536e80 Compare November 26, 2026 06:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants