Skip to content
Prev Previous commit
ci(cleanup): count the nested retain's deferred roots apart from the …
…serial sweep's

The Amplify step now deletes one sandbox at a time (#1255) and counts
the sandboxes it defers to the next run in DEFERRED. This PR counted the
roots it defers after starting a nested retain in a variable of the same
name, so both summary lines printed the sum. It now has its own counter,
NESTED_DEFERRED.

The nested retain stays behind the one-at-a-time gate, so it never
starts while an earlier sandbox's delete may still be running, and its
nested delete is not awaited: the model stack holds no API Gateway
resource, so it doesn't compete for the REST API delete quota the wait
protects. A comment in the step says so.

Tests: a stuck and a normal sandbox in one run (the stuck root is
deferred on its own line, the normal one deleted and awaited), and no
nested delete once the wait budget is spent or while an earlier delete
is still running. The step tests use the step's poll override, so they
take seconds, not minutes.
  • Loading branch information
belaboe committed Nov 5, 2026
commit b3731586679a97f444e835ac75bacf7acb55404c
19 changes: 14 additions & 5 deletions .github/workflows/cleanup-stacks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ jobs:
# at 40 min and defer the remainder to the next scheduled run.
DEADLINE=$((SECONDS + 2400)) # 40 minutes
# Of that, at most 15 minutes waiting on deletes (see the delete loop).
# The wait settings are overridable for scripts/ci/amplify-sweep.test.mjs.
# The wait settings are overridable for the step tests
# (scripts/ci/amplify-sweep.test.mjs, amplify-stuck-delete.test.mjs).
WAIT_DEADLINE=$((SECONDS + ${AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS:-900}))
PER_STACK_WAIT=${AMPLIFY_SWEEP_STACK_WAIT_SECONDS:-360}
POLL=${AMPLIFY_SWEEP_POLL_SECONDS:-15}
Expand All @@ -421,7 +422,7 @@ jobs:
DEFERRED=0
BLOCKED=""
TRIGGER_FAILED=0
DEFERRED=0
NESTED_DEFERRED=0
SCRIPT_FAILED=0
RETAINED_FILE=$(mktemp)

Expand Down Expand Up @@ -564,6 +565,14 @@ jobs:
# for deletion by hand, on this run and every later one (--leftovers
# below). The script runs from the job's checkout, which includes
# scripts/ci on every leg.
#
# It runs behind the one-at-a-time gate above, so it never starts while
# an earlier root delete may still be running. Its own nested delete is
# not awaited (so it can overlap a later root delete, or another nested
# one): the model stack holds the AppSync resolvers, IAM roles and the
# table, but no API Gateway resource, so it doesn't compete for the REST
# API delete quota the wait protects. The root, whose `blocks` nested
# stack holds the REST API, is deleted (and awaited) on the next run.
echo "🧹 Emptying nested buckets for $STACK"
_VISITED_STACKS=""
empty_stack_buckets "$STACK"
Expand All @@ -574,7 +583,7 @@ jobs:
if [ "$RC" -eq 3 ]; then
# Not deleting the root now: its delete would race the nested one.
echo "Nested stack delete started for $STACK; root delete deferred to the next run"
DEFERRED=$((DEFERRED + 1))
NESTED_DEFERRED=$((NESTED_DEFERRED + 1))
continue
elif [ "$RC" -ne 0 ]; then
echo "::warning::amplify-stuck-delete.sh exited $RC for $STACK; plain retry only"
Expand Down Expand Up @@ -630,7 +639,7 @@ jobs:
echo "- Deleted (confirmed): $DELETED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Deferred to the next run: $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Delete-stack call failed: $TRIGGER_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Root delete deferred (nested stack delete started): $DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Root delete deferred (nested stack delete started): $NESTED_DEFERRED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Nested retain script failed: $SCRIPT_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
echo "- Still DELETE_FAILED: $STILL_FAILED" | tee -a "$GITHUB_STEP_SUMMARY"
# What amplify-stuck-delete.sh retained, this run or earlier (with the
Expand All @@ -651,7 +660,7 @@ jobs:
echo "" | tee -a "$GITHUB_STEP_SUMMARY"
echo "_Each delete is awaited (bounded) before the next; stacks that don't finish (commonly a throttled REST API delete, or the Amplify managed DynamoDB table) are retried on the next scheduled run._" | tee -a "$GITHUB_STEP_SUMMARY"
if [ "$STILL_FAILED" != unknown ] && [ "$STILL_FAILED" -gt 0 ]; then
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
echo "::warning::$STILL_FAILED Amplify sandbox stack(s) still in DELETE_FAILED after cleanup, nested ones included ($NESTED_DEFERRED root(s) deferred after a nested retain); AppSync API quota may be at risk"
fi

- name: Cleanup orphaned API Gateway REST APIs
Expand Down
52 changes: 50 additions & 2 deletions scripts/ci/amplify-stuck-delete.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,17 @@ function stepScript() {
* Runs the step against `files`, with the root listed, tagged and `status`.
* `cwd` replaces the repo root the step runs scripts/ci/ from.
*/
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd } = {}) {
function runStep(files, { status = 'DELETE_FAILED', created = '2026-10-19T14:48:22.894Z', cwd, env = {} } = {}) {
const dir = fixture({
'stacks.txt': `${ROOT}\n`,
[`info-${key(ROOT)}.txt`]: `sandbox\t${status}\t${created}\n`,
...files,
});
const file = join(dir, 'step.sh');
writeFileSync(file, stepScript());
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '' }, cwd);
// The step waits for each plain delete (polling describe-stacks); no real
// sleeps in tests.
return run(dir, [file], { AMPLIFY_RETAINED_FILE: '', AMPLIFY_SWEEP_POLL_SECONDS: '0', ...env }, cwd);
}

const NO_ACCOUNT = /123456789012/;
Expand Down Expand Up @@ -807,6 +809,52 @@ describe(`cleanup-stacks.yml "${STEP}" step`, { skip: !GNU_DATE && 'needs GNU da
assert.doesNotMatch(r.summary, NO_ACCOUNT);
});

// The step deletes one sandbox at a time and defers the rest once its wait
// budget is spent or a delete is still running. The nested retain runs behind
// that gate, and its deferred roots are counted on their own line.
const NORMAL = 'amplify-amplifygen2-amp716639251-sandbox-1111111111';
const two = (over = {}) =>
sandbox({
'stacks.txt': `${ROOT}\n${NORMAL}\n`,
[`info-${key(NORMAL)}.txt`]: 'sandbox\tROLLBACK_COMPLETE\t2026-10-19T14:48:22.894Z\n',
...over,
});

it('defers a stuck root on its own count, then deletes a normal sandbox and waits for it', () => {
const r = runStep(two());
assert.equal(r.status, 0, r.stdout);
assert.deepEqual(r.deletes, [RETAIN_TODO, `cloudformation delete-stack --stack-name ${NORMAL}`]);
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 1$/m);
assert.match(r.summary, /^- Deferred to the next run: 0$/m);
assert.match(r.summary, /^- Deleted \(confirmed\): 1$/m);
});

it('starts no nested delete once the wait budget is spent', () => {
const r = runStep(two(), { env: { AMPLIFY_SWEEP_WAIT_BUDGET_SECONDS: '0' } });
assert.equal(r.status, 0, r.stdout);
assert.deepEqual(r.deletes, []);
assert.match(r.summary, /^- Deferred to the next run: 2$/m);
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
});

it('starts no nested delete while an earlier sandbox is still deleting', () => {
// The normal sandbox comes first, and every poll in its 1-second wait
// reads DELETE_IN_PROGRESS.
const r = runStep(
two({
'stacks.txt': `${NORMAL}\n${ROOT}\n`,
[`status-${key(NORMAL)}.txt`]: 'DELETE_IN_PROGRESS\n',
}),
{ env: { AMPLIFY_SWEEP_STACK_WAIT_SECONDS: '1', AMPLIFY_SWEEP_POLL_SECONDS: '0.2' } },
);
assert.equal(r.status, 0, r.stdout);
assert.match(r.calls, new RegExp(`describe-stacks --stack-name ${NORMAL} --query Stacks\\[0\\]\\.StackStatus`));
assert.match(r.stdout, new RegExp(`Still deleting: ${NORMAL}`));
assert.deepEqual(r.deletes, [`cloudformation delete-stack --stack-name ${NORMAL}`]);
assert.match(r.summary, /^- Deferred to the next run: 1$/m);
assert.match(r.summary, /^- Root delete deferred \(nested stack delete started\): 0$/m);
});

it('only retries plainly a ROLLBACK_FAILED sandbox (the retain needs DELETE_FAILED)', () => {
const r = runStep(sandbox(), { status: 'ROLLBACK_FAILED' });
assert.equal(r.status, 0, r.stdout);
Expand Down
Loading