diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 52b458f692ffa..85079f6a0c0dd 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -558,16 +558,22 @@ export class CdkToolkit { // The stacks will have been ordered for deployment, so reverse them for deletion. stacks = stacks.reversed(); + const artifacts = stacks.stackArtifacts; + if (!options.force) { // eslint-disable-next-line max-len - const confirmed = await promptly.confirm(`Are you sure you want to delete: ${chalk.blue(stacks.stackArtifacts.map(s => s.hierarchicalId).join(', '))} (y/n)?`); + const confirmed = await promptly.confirm(`Are you sure you want to delete: ${chalk.blue(artifacts.map(s => s.hierarchicalId).join(', '))} (y/n)?`); if (!confirmed) { return; } } + if (artifacts.length === 0) { + throw new Error('Stack does not exist'); + } + const action = options.fromDeploy ? 'deploy' : 'destroy'; - for (const [index, stack] of stacks.stackArtifacts.entries()) { + for (const [index, stack] of artifacts.entries()) { success('%s: destroying... [%s/%s]', chalk.blue(stack.displayName), index+1, stacks.stackCount); try { await this.props.deployments.destroyStack({ diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index bb84e0fd6b4a7..f661d2b579d3c 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -602,6 +602,19 @@ describe('destroy', () => { }); }).resolves; }); + + test('fail on non-existent stack', async () => { + const toolkit = defaultToolkitSetup(); + + await expect(() => { + return toolkit.destroy({ + selector: { patterns: ['Test-Stack-X'] }, + exclusively: true, + force: true, + fromDeploy: true, + }); + }).rejects.toThrowError('Stack does not exist'); + }); }); describe('watch', () => {