RemovalPolicyProps

class aws_cdk.RemovalPolicyProps(*, apply_to_resource_types=None, exclude_resource_types=None, priority=None)

Bases: object

Properties for applying a removal policy.

Parameters:
  • apply_to_resource_types (Optional[Sequence[str]]) – Apply the removal policy only to specific resource types. Can be a CloudFormation resource type string (e.g., ‘AWS::S3::Bucket’). Default: - apply to all resources

  • exclude_resource_types (Optional[Sequence[str]]) – Exclude specific resource types from the removal policy. Can be a CloudFormation resource type string (e.g., ‘AWS::S3::Bucket’). Default: - no exclusions

  • priority (Union[int, float, None]) – The priority to use when applying this policy. The priority affects only the order in which aspects are applied during synthesis. For RemovalPolicies, the last applied policy will override previous ones. NOTE: Priority does NOT determine which policy “wins” when there are conflicts. The order of application determines the final policy, with later policies overriding earlier ones. Default: - AspectPriority.MUTATING

ExampleMetadata:

infused

Example:

# scope: Construct
# parent: Construct
# bucket: s3.CfnBucket


# Apply DESTROY policy to all resources in a scope
RemovalPolicies.of(scope).destroy()

# Apply RETAIN policy to all resources in a scope
RemovalPolicies.of(scope).retain()

# Apply SNAPSHOT policy to all resources in a scope
RemovalPolicies.of(scope).snapshot()

# Apply RETAIN_ON_UPDATE_OR_DELETE policy to all resources in a scope
RemovalPolicies.of(scope).retain_on_update_or_delete()

# Apply RETAIN policy only to specific resource types
RemovalPolicies.of(parent).retain(
    apply_to_resource_types=["AWS::DynamoDB::Table", bucket.cfn_resource_type, rds.CfnDBInstance.CFN_RESOURCE_TYPE_NAME
    ]
)

# Apply SNAPSHOT policy excluding specific resource types
RemovalPolicies.of(scope).snapshot(
    exclude_resource_types=["AWS::Test::Resource"]
)

Attributes

apply_to_resource_types

Apply the removal policy only to specific resource types.

Can be a CloudFormation resource type string (e.g., ‘AWS::S3::Bucket’).

Default:
  • apply to all resources

exclude_resource_types

Exclude specific resource types from the removal policy.

Can be a CloudFormation resource type string (e.g., ‘AWS::S3::Bucket’).

Default:
  • no exclusions

priority

The priority to use when applying this policy.

The priority affects only the order in which aspects are applied during synthesis. For RemovalPolicies, the last applied policy will override previous ones.

NOTE: Priority does NOT determine which policy “wins” when there are conflicts. The order of application determines the final policy, with later policies overriding earlier ones.

Default:
  • AspectPriority.MUTATING