{"record":{"id":"ed7444071b0c4279","repo":"heygen-com/hyperframes","slug":"lambda-sam-delete-exited-with-code-result-stat","errorCode":null,"errorMessage":"[lambda] sam delete exited with code ${result.status ?? \"unknown\"}","messagePattern":"\\[lambda\\] sam delete exited with code (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/lambda/sam.ts","lineNumber":136,"sourceCode":"}\n\n/** Run `sam delete` non-interactively. */\nexport function samDelete(opts: {\n  repoRoot: string;\n  stackName: string;\n  region: string;\n  awsProfile?: string;\n  stdio?: \"inherit\" | \"pipe\";\n}): void {\n  assertSamAvailable();\n  const args = [\"delete\", \"--stack-name\", opts.stackName, \"--region\", opts.region, \"--no-prompts\"];\n  if (opts.awsProfile) {\n    args.push(\"--profile\", opts.awsProfile);\n  }\n  const samDir = join(opts.repoRoot, \"examples\", \"aws-lambda\");\n  const result = spawnSync(\"sam\", args, { cwd: samDir, stdio: opts.stdio ?? \"inherit\" });\n  if (result.status !== 0) {\n    throw new Error(`[lambda] sam delete exited with code ${result.status ?? \"unknown\"}`);\n  }\n}\n\nexport interface StackOutputBag {\n  bucketName: string;\n  functionName: string;\n  stateMachineArn: string;\n}\n\n/**\n * Query CloudFormation for the stack outputs the SAM template exports.\n * Used after `samDeploy` to populate the local state file.\n */\nexport function fetchStackOutputs(opts: {\n  stackName: string;\n  region: string;\n  awsProfile?: string;\n}): StackOutputBag {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/heygen-com/hyperframes/blob/c2996c8626135db5253519359d8a063d3bafad8d/packages/cli/src/commands/lambda/sam.ts#L118-L154","documentation":"Thrown by samDelete() in packages/cli/src/commands/lambda/sam.ts:135 when `sam delete --no-prompts` exits non-zero. Unlike the deploy error, no diagnostic hint is appended — the message reports only the exit code, so the operator must consult the streamed SAM output for the cause.","triggerScenarios":"samDelete against a stack that does not exist in the given region, with the wrong profile, with insufficient delete permissions, or when the installed SAM CLI version rejects the --no-prompts flag.","commonSituations":"Stack already deleted (or never created) in that region; --region mismatch with where the stack lives; AWS credentials lack cloudformation:DeleteStack; SAM CLI version drift.","solutions":["Confirm the stack exists in the region: `aws cloudformation describe-stacks --stack-name <name> --region <region>`.","Check the SAM output streamed to stderr/stdout above the thrown error for the precise failure.","Verify --aws-profile has delete permissions and the region matches the deploy region.","If SAM keeps failing, fall back to `aws cloudformation delete-stack --stack-name <name> --region <region>` directly."],"exampleFix":"# before\nhyperframes lambda destroy   # exits non-zero on missing stack\n# after\naws cloudformation describe-stacks --stack-name <name> --region us-east-1\n# if not found, nothing to delete; otherwise:\naws cloudformation delete-stack --stack-name <name> --region us-east-1","handlingStrategy":"try-catch","validationCode":"import { execFileSync } from 'node:child_process';\n\nfunction stackExists(name: string, region: string, profile?: string): boolean {\n  const args = ['cloudformation', 'describe-stacks', '--stack-name', name, '--region', region];\n  if (profile) args.unshift('--profile', profile);\n  try {\n    execFileSync('aws', args, { stdio: 'ignore' });\n    return true;\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  samDelete(opts);\n} catch (error) {\n  if (/does not exist/i.test(String(error)) || /ValidationError/.test(String(error))) {\n    // stack already gone — treat as success\n    return;\n  }\n  throw error;\n}","preventionTips":["Check stack existence in the region before calling destroy.","Treat 'stack does not exist' as a no-op success in destroy wrappers.","Confirm the SAM CLI version installed accepts --no-prompts."],"tags":["aws","lambda","deployment","cloudformation","sam"],"backgroundTag":null,"analyzedSha":"c2996c8626135db5253519359d8a063d3bafad8d","analyzedAt":"2026-08-12T22:18:56.877Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}