Skip to content
v2.278.0

RemovalPolicy Enum ​

Possible values for a resource's Removal Policy.

The removal policy controls what happens to the resource if it stops being managed by CloudFormation. This can happen in one of three situations:

  • The resource is removed from the template, so CloudFormation stops managing it;
  • A change to the resource is made that requires it to be replaced, so CloudFormation stops managing it;
  • The stack is deleted, so CloudFormation stops managing all resources in it.

The Removal Policy applies to all above cases.

Many stateful resources in the AWS Construct Library will accept a removalPolicy as a property, typically defaulting it to RETAIN.

If the AWS Construct Library resource does not accept a removalPolicy argument, you can always configure it by using the escape hatch mechanism, as shown in the following example:

ts
declare const bucket: s3.Bucket;

const cfnBucket = bucket.node.findChild('Resource') as CfnResource;
cfnBucket.applyRemovalPolicy(RemovalPolicy.DESTROY);

Example ​

ts
declare const myRole: iam.Role;
new cr.AwsCustomResource(this, 'Customized', {
  role: myRole, // must be assumable by the `lambda.amazonaws.com` service principal
  timeout: Duration.minutes(10), // defaults to 2 minutes
  memorySize: 1025, // defaults to 512 if installLatestAwsSdk is true
  logGroup: new logs.LogGroup(this, 'AwsCustomResourceLogs', {
    retention: logs.RetentionDays.ONE_DAY,
  }),
  functionName: 'my-custom-name', // defaults to a CloudFormation generated name
  removalPolicy: RemovalPolicy.RETAIN, // defaults to `RemovalPolicy.DESTROY`
  policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
    resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
  }),
});
python
# my_role: iam.Role

cr.AwsCustomResource(self, "Customized",
    role=my_role,  # must be assumable by the `lambda.amazonaws.com` service principal
    timeout=Duration.minutes(10),  # defaults to 2 minutes
    memory_size=1025,  # defaults to 512 if installLatestAwsSdk is true
    log_group=logs.LogGroup(self, "AwsCustomResourceLogs",
        retention=logs.RetentionDays.ONE_DAY
    ),
    function_name="my-custom-name",  # defaults to a CloudFormation generated name
    removal_policy=RemovalPolicy.RETAIN,  # defaults to `RemovalPolicy.DESTROY`
    policy=cr.AwsCustomResourcePolicy.from_sdk_calls(
        resources=cr.AwsCustomResourcePolicy.ANY_RESOURCE
    )
)
java
Role myRole;

AwsCustomResource.Builder.create(this, "Customized")
        .role(myRole) // must be assumable by the `lambda.amazonaws.com` service principal
        .timeout(Duration.minutes(10)) // defaults to 2 minutes
        .memorySize(1025) // defaults to 512 if installLatestAwsSdk is true
        .logGroup(LogGroup.Builder.create(this, "AwsCustomResourceLogs")
                .retention(RetentionDays.ONE_DAY)
                .build())
        .functionName("my-custom-name") // defaults to a CloudFormation generated name
        .removalPolicy(RemovalPolicy.RETAIN) // defaults to `RemovalPolicy.DESTROY`
        .policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder()
                .resources(AwsCustomResourcePolicy.ANY_RESOURCE)
                .build()))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Role myRole;

new AwsCustomResource(this, "Customized", new AwsCustomResourceProps {
    Role = myRole,  // must be assumable by the `lambda.amazonaws.com` service principal
    Timeout = Duration.Minutes(10),  // defaults to 2 minutes
    MemorySize = 1025,  // defaults to 512 if installLatestAwsSdk is true
    LogGroup = new LogGroup(this, "AwsCustomResourceLogs", new LogGroupProps {
        Retention = RetentionDays.ONE_DAY
    }),
    FunctionName = "my-custom-name",  // defaults to a CloudFormation generated name
    RemovalPolicy = RemovalPolicy.RETAIN,  // defaults to `RemovalPolicy.DESTROY`
    Policy = AwsCustomResourcePolicy.FromSdkCalls(new SdkCallsPolicyOptions {
        Resources = AwsCustomResourcePolicy.ANY_RESOURCE
    })
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var myRole Role

cr.NewAwsCustomResource(this, jsii.String("Customized"), &AwsCustomResourceProps{
	Role: myRole,
	 // must be assumable by the `lambda.amazonaws.com` service principal
	Timeout: awscdk.Duration_Minutes(jsii.Number(10)),
	 // defaults to 2 minutes
	MemorySize: jsii.Number(1025),
	 // defaults to 512 if installLatestAwsSdk is true
	LogGroup: logs.NewLogGroup(this, jsii.String("AwsCustomResourceLogs"), &LogGroupProps{
		Retention: logs.RetentionDays_ONE_DAY,
	}),
	FunctionName: jsii.String("my-custom-name"),
	 // defaults to a CloudFormation generated name
	RemovalPolicy: awscdk.RemovalPolicy_RETAIN,
	 // defaults to `RemovalPolicy.DESTROY`
	Policy: cr.AwsCustomResourcePolicy_FromSdkCalls(&SdkCallsPolicyOptions{
		Resources: cr.AwsCustomResourcePolicy_ANY_RESOURCE(),
	}),
})

Members ​

MemberDescription
DESTROYWhen this removal policy is applied, the resource will be physically destroyed when it is removed from the stack or when the stack is deleted.
RETAINThis uses the 'Retain' DeletionPolicy, which will cause the resource to be retained in the account, but orphaned from the stack.
SNAPSHOTThis retention policy deletes the resource, but saves a snapshot of its data before deleting, so that it can be re-created later.
RETAIN_ON_UPDATE_OR_DELETEResource will be retained when they are requested to be deleted during a stack delete request or need to be replaced due to a stack update request.

Referenced by ​