diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 724e0f02b579a..6652c5feda6e0 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -179,7 +179,7 @@ export class CdkToolkit { } else { warning('%s: stack has no resources, deleting existing stack.', chalk.bold(stack.displayName)); await this.destroy({ - selector: { patterns: [stack.stackName] }, + selector: { patterns: [stack.hierarchicalId] }, exclusively: true, force: true, roleArn: options.roleArn, diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index 0028f58d3544b..341775b08b38a 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -56,7 +56,7 @@ jest.mock('../lib/logging', () => ({ import * as cxschema from '@aws-cdk/cloud-assembly-schema'; import * as cxapi from '@aws-cdk/cx-api'; import { Bootstrapper } from '../lib/api/bootstrap'; -import { CloudFormationDeployments, DeployStackOptions } from '../lib/api/cloudformation-deployments'; +import { CloudFormationDeployments, DeployStackOptions, DestroyStackOptions } from '../lib/api/cloudformation-deployments'; import { DeployStackResult } from '../lib/api/deploy-stack'; import { Template } from '../lib/api/util/cloudformation'; import { CdkToolkit, Tag } from '../lib/cdk-toolkit'; @@ -558,6 +558,21 @@ describe('deploy', () => { }); }); +describe('destroy', () => { + test('destroy correct stack', async () => { + const toolkit = defaultToolkitSetup(); + + await expect(() => { + return toolkit.destroy({ + selector: { patterns: ['Test-Stack-A/Test-Stack-C'] }, + exclusively: true, + force: true, + fromDeploy: true, + }); + }).resolves; + }); +}); + describe('watch', () => { test("fails when no 'watch' settings are found", async () => { const toolkit = defaultToolkitSetup(); @@ -919,6 +934,11 @@ class FakeCloudFormation extends CloudFormationDeployments { }); } + public destroyStack(options: DestroyStackOptions): Promise { + expect(options.stack).toBeDefined(); + return Promise.resolve(); + } + public readCurrentTemplate(stack: cxapi.CloudFormationStackArtifact): Promise