Skip to main content
Pulumi logo Pulumi logo
  1. Docs
  2. Infrastructure as Code
  3. Concepts
  4. Resources
  5. Resource options
  6. retainOnDelete

Resource option: retainOnDelete

    The retainOnDelete resource option marks a resource to be retained. If this option is set, Pulumi does not call through to the resource provider’s Delete method when deleting or replacing the resource during pulumi up or pulumi destroy. As a result, the resource is not deleted from the backing cloud provider, but is removed from the Pulumi state.

    Applies to custom resources only. The retainOnDelete resource option has no direct effect on component resources. It is defined on the base resource-options type in every Pulumi SDK, so no SDK rejects it at compile time — passing it to a component resource has no direct effect at runtime. Setting retainOnDelete on a component has no direct effect — components don't have cloud resources to retain. The value propagates to every child custom resource of the component, which is the idiomatic way to retain a whole subtree.

    For a complete walkthrough of using this option to remove a resource from a stack without deleting the underlying infrastructure, including how it compares to pulumi state delete, see Removing resources without deleting them.

    const db = new Database("db", {}, { retainOnDelete: true });
    
    db = Database("db", opts=ResourceOptions(retain_on_delete=True))
    
    db, _ := NewDatabase(ctx, "db", &DatabaseArgs{}, pulumi.RetainOnDelete(true))
    
    var db = new Database("db", new DatabaseArgs(),
        new CustomResourceOptions { RetainOnDelete = true });
    
    var db = new Database("db",
        DatabaseArgs.Empty,
        CustomResourceOptions.builder()
            .retainOnDelete(true)
            .build());
    
    resources:
      db:
        type: Database
        options:
          retainOnDelete: true
    
    resource "database" "db" {
      # ...
    
      pulumi {
        retain_on_delete = true
      }
    }
    

    Deleting a retained resource

    To delete a retained resource from the backing cloud provider, first set retainOnDelete: false and run pulumi up. Once the resource is no longer marked retained, a later update that removes it, or pulumi destroy, deletes it fully.

    If Pulumi has already removed a retained resource from the stack, the resource still exists in the cloud provider. To delete it, either use the provider’s console or CLI, or import it back into Pulumi, unset retainOnDelete, and delete it again.

    Inheritance from parent

    retainOnDelete is inherited from a resource’s parent, and defaults to false for resources without a parent. Setting retainOnDelete on a parent retains every descendant in the resource tree, which makes it the way to retain all of the resources in a component resource.

    Because a component has no infrastructure of its own in the cloud provider, setting retainOnDelete on a component has no direct effect on the component. Its effect comes entirely from propagating the value to the component’s children.

    A child can override an inherited value by setting retainOnDelete explicitly. For example, set retainOnDelete: false on a child to delete it normally even though its parent is retained. For the full list of options children inherit, see Inherited resource options.

      The infrastructure as code platform for any cloud.