Skip to content

ci(cleanup): pace stack deletes that remove a REST API; retain stuck stacks by time in DELETE_FAILED - #1365

Open
xyos wants to merge 3 commits into
mainfrom
ci/cleanup-paced-deletes
Open

xyos wants to merge 3 commits into
mainfrom
ci/cleanup-paced-deletes

Conversation

@xyos

@xyos xyos commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The scheduled stack sweep (cleanup-stacks.yml, every 2 h, one leg per E2E account) triggers delete-stack for every stale stack at once. Each Hosting stack owns a REST API, and API Gateway allows about one DeleteRestApi per 30 seconds per account. So a backlog of 50–100 stacks throttled its own deletes and the PR teardowns running in the same account, and most stacks ended DELETE_FAILED again. Over 2026-11-02/03, account 1's DELETE_FAILED backlog went 3 → 51 → 81 → 109 stacks.

The stuck-stack retry that retains blocking resources never ran: in the sweep logs since 2026-11-01 it was attempted 341 times on 147 stacks, and CloudFormation rejected every one with A delete stack operation is already in progress, because a plain delete-stack of the same stack always went first.

Issue #, if available:

Changes

  • Pacing: deletes of stacks that still own a REST API (nested stacks included) are spaced at least 31 s apart, like the existing orphaned REST API sweep, with at most 25 per run (about 13 minutes, inside the step's 40-minute budget). The rest wait for the next run. Stacks without a REST API aren't paced.
  • Unknown ownership: a stack whose resources (or a live nested stack's) can't be listed is deferred with a warning that gives the CLI's reason, instead of being deleted unpaced. A nested stack that is already DELETE_COMPLETE is not walked.
  • In-progress deletes: a stack whose delete is already in progress is skipped.
  • Retain path: whether to retain resources now depends on how long the stack has been DELETE_FAILED (its newest stack-level DELETE_FAILED event, over an hour), not on being created more than 6 h ago. The retaining delete is the stack's only delete call in that run.
  • Retain only what a sweep reclaims: a failed REST API is retained only if its name passes the REST API sweep's name gate, so that sweep later deletes it. Others, such as hosting's SsrRestApi, get the plain retry, and a failed name lookup retains nothing. e2e-stack-prefixes.test.mjs checks that the two name gates match.
  • ci(cleanup): fail the sweep leg when REST APIs can't be counted #959's fail-loud counting and ci(cleanup): unblock Amplify sandboxes stuck on a cancelled table create #970's Amplify table retain are unchanged; the table retain is also its stack's only delete call in a run.
  • New scripts/ci/stack-sweep.test.mjs runs the step's script against stub aws and sleep commands, and Local Build and Checks runs it.

Validation

  • scripts/ci/stack-sweep.test.mjs covers: pacing and the per-run cap, in-progress skip, the retain basis, retain-as-only-delete (including a rejected retain), the name gate (an SsrRestApi blocker isn't retained; mixed blockers; a failed lookup), failed top-level and nested listings deferring the stack, a deleted nested stack not blocking its parent, and ci(cleanup): unblock Amplify sandboxes stuck on a cancelled table create #970's table retain. Against main's step, 13 of its 15 cases fail; the 2 that pass cover behaviour this PR keeps.
  • node --test scripts/ci/*.test.mjs scripts/check-actions-pinned.test.mjs: 504 pass, 0 fail, 12 skipped.
  • actionlint reports nothing on cleanup-stacks.yml.
  • Not run against AWS: whether 31 s spacing between delete-stack calls spreads the REST API deletes as intended, and how fast a backlog drains. The first scheduled runs after merge will show it in the step summary (triggered / deferred / retained counts).

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…stacks by time in DELETE_FAILED

The scheduled stack sweep triggered delete-stack for every stale stack at
once. Each Hosting stack owns a REST API, and API Gateway allows about one
DeleteRestApi per 30 seconds per account, so a backlog of 50-100 stacks
throttled its own deletes and the PR teardowns running in the same account,
and most stacks ended DELETE_FAILED again.

- Space deletes of stacks that still own a REST API (nested stacks
  included) at least 31s apart, like the REST API sweep, and trigger at
  most 25 per run; the rest wait for the next run. Stacks without a REST
  API are not paced. A stack whose resources (or a live nested stack's)
  cannot be listed is deferred too, with a warning that gives the CLI's
  reason, since whether it owns a REST API is unknown; a nested stack
  already deleted is not walked.
- Skip a stack whose delete is already in progress.
- Decide the --retain-resources delete from how long the stack has been
  DELETE_FAILED (its newest stack-level DELETE_FAILED event, over an hour)
  instead of its creation time, and issue it as the stack's only delete.
  It used to follow a plain delete-stack, which CloudFormation rejects
  with "A delete stack operation is already in progress", so the retain
  path never ran.
- Retain a failed REST API only when its name passes the REST API sweep's
  name gate, so the sweep reclaims it; others (such as hosting's
  SsrRestApi) get the plain retry, and a failed name lookup retains
  nothing. A delete that retains every REST API its stack owns is not
  paced.
- Add scripts/ci/stack-sweep.test.mjs, which runs the step against a stub
  aws and sleep, run it in PR checks, and check in
  e2e-stack-prefixes.test.mjs that the retain and sweep name gates match.
  Cover the Amplify step's nested table retain the same way: a rejected
  retaining delete gets no plain delete of the same stack in that run.
@xyos
xyos requested a review from a team as a code owner November 8, 2026 10:24
@changeset-bot

changeset-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a381fe4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

This branch was previously deployed

1 inactive deployment
e2e — a381fe49 Deployed Nov 10, 2026 by xyos via E2E Amplify Interop (Browser) #5892
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant