11import { CONFIG_DIR , ConfigIO } from '../../../lib' ;
22import type { AwsDeploymentTarget } from '../../../schema' ;
33import { withTargetRegion } from '../../aws' ;
4+ import { deleteABTest } from '../../aws/agentcore-ab-tests' ;
5+ import { deleteConfigurationBundle } from '../../aws/agentcore-config-bundles' ;
46import { CdkToolkitWrapper , silentIoHost } from '../../cdk/toolkit-lib' ;
57import { type DiscoveredStack , findStack } from '../../cloudformation/stack-discovery' ;
68import { deleteHttpGatewayWithTargets } from './post-deploy-http-gateways' ;
@@ -113,11 +115,15 @@ export async function performStackTeardown(targetName: string): Promise<StackTea
113115 const discovered = await discoverDeployedTargets ( ) ;
114116 const deployedTarget = discovered . deployedTargets . find ( dt => dt . target . name === targetName ) ;
115117
116- // Clean up imperatively-created HTTP gateways before stack destruction
118+ // Clean up imperatively-created resources before stack destruction
117119 try {
118120 const deployedState = await configIO . readDeployedState ( ) ;
119- const httpGateways = deployedState . targets ?. [ targetName ] ?. resources ?. httpGateways ;
120- if ( httpGateways ) {
121+ const resources = deployedState . targets ?. [ targetName ] ?. resources ;
122+ const httpGateways = resources ?. httpGateways ;
123+ const configBundles = resources ?. configBundles ;
124+ const abTests = resources ?. abTests ;
125+
126+ if ( httpGateways || configBundles || abTests ) {
121127 let region = deployedTarget ?. target . region ;
122128 if ( ! region ) {
123129 try {
@@ -129,12 +135,10 @@ export async function performStackTeardown(targetName: string): Promise<StackTea
129135 }
130136 }
131137 if ( ! region ) {
132- console . warn (
133- 'Warning: Could not determine region for HTTP gateway cleanup — gateways may need manual deletion'
134- ) ;
138+ console . warn ( 'Warning: Could not determine region for resource cleanup — resources may need manual deletion' ) ;
135139 }
136140 if ( region ) {
137- for ( const [ gwName , gwState ] of Object . entries ( httpGateways ) ) {
141+ for ( const [ gwName , gwState ] of Object . entries ( httpGateways ?? { } ) ) {
138142 try {
139143 const result = await deleteHttpGatewayWithTargets ( {
140144 region,
@@ -155,13 +159,39 @@ export async function performStackTeardown(targetName: string): Promise<StackTea
155159 ) ;
156160 }
157161 }
162+
163+ for ( const [ bundleName , bundleState ] of Object . entries ( configBundles ?? { } ) ) {
164+ try {
165+ await deleteConfigurationBundle ( { region, bundleId : bundleState . bundleId } ) ;
166+ console . log ( `Deleted config bundle "${ bundleName } "` ) ;
167+ } catch ( err ) {
168+ console . warn (
169+ `Warning: Error during config bundle "${ bundleName } " cleanup: ${ err instanceof Error ? err . message : String ( err ) } `
170+ ) ;
171+ }
172+ }
173+
174+ for ( const [ testName , testState ] of Object . entries ( abTests ?? { } ) ) {
175+ try {
176+ const result = await deleteABTest ( { region, abTestId : testState . abTestId } ) ;
177+ if ( result . success ) {
178+ console . log ( `Deleted AB test "${ testName } "` ) ;
179+ } else {
180+ console . warn ( `Warning: Failed to delete AB test "${ testName } ": ${ result . error } ` ) ;
181+ }
182+ } catch ( err ) {
183+ console . warn (
184+ `Warning: Error during AB test "${ testName } " cleanup: ${ err instanceof Error ? err . message : String ( err ) } `
185+ ) ;
186+ }
187+ }
158188 }
159189 }
160190 } catch ( err ) {
161191 // Only suppress "file not found" — other errors (corrupt state, permissions) should warn
162192 const msg = err instanceof Error ? err . message : String ( err ) ;
163193 if ( ! msg . includes ( 'ENOENT' ) && ! msg . includes ( 'not found' ) && ! msg . includes ( 'does not exist' ) ) {
164- console . warn ( `Warning: Could not read deployed state for HTTP gateway cleanup: ${ msg } ` ) ;
194+ console . warn ( `Warning: Could not read deployed state for resource cleanup: ${ msg } ` ) ;
165195 }
166196 }
167197
0 commit comments