Infrastructure Manager lets you remove deployments that you no longer need. Deleting a
deployment removes its metadata and state, with the option to either delete or
retain the underlying provisioned resources.

This document assumes that you are familiar with Terraform. For details, see
[Terraform and Infra Manager](https://docs.cloud.google.com/infrastructure-manager/docs/infra-manager-terraform.md.txt).

## Before you begin

1. Ensure [Infra Manager is enabled](https://docs.cloud.google.com/infrastructure-manager/docs/enable-service.md.txt).
2. Ensure that you have the IAM permissions needed to create deployments: `roles/config.admin`.
3. Ensure the service account that was last used to create or update the deployment is still valid. Infra Manager uses this service account to delete the deployment.
   - For details about configuring the service account, including setting up required permissions, see [Configure the service account](https://docs.cloud.google.com/infrastructure-manager/docs/configure-service-account.md.txt).
   - To determine the service account that was last used for the deployment, see [View the state of a revision](https://docs.cloud.google.com/infrastructure-manager/docs/view-deployments.md.txt#revision-details).
4. Ensure the Terraform configuration that Infra Manager used to create or update the deployment is still valid. Infra Manager uses this configuration to delete the deployment.
5. Identify the Infra Manager deployment to delete.
6. Ensure that the service account has the IAM permissions needed to delete resources in the specified projects and locations.

## Grant permissions specific to the configuration

To delete Google Cloud resources in a specific deployment, the service
account that you use to call Infra Manager needs
permissions that are specific to what you are deleting.

These permissions are in addition to the permissions needed to use the
Infra Manager service.

For details about granting permissions for the Google Cloud resources and
ensuring you have access to the needed projects, see
[Configure the service account](https://docs.cloud.google.com/infrastructure-manager/docs/configure-service-account.md.txt).

## Delete a deployment

When an Infra Manager deployment is created, Infra Manager
stores metadata about the deployment, the Terraform configuration that is
deployed, and the state file.

When you delete a deployment, you delete this metadata and files. You can
also delete the resources provisioned by the deployment, or keep these
resources.

### Delete provisioned resources

> [!CAUTION]
> **Caution:** Deleting Google Cloud resources can cause unrecoverable data loss. Re-deploying the configuration won't restore data.

To delete the deployment metadata and delete the provisioned resources:

1. Optionally, list the resources provisioned by the deployment:

   ```bash
   gcloud infra-manager resources list --revision=projects/PROJECT_ID/locations/LOCATION/deployments/DEPLOYMENT_ID/revisions/REVISION_ID
   ```

   Replace the following:
   - <var translate="no">PROJECT_ID</var>: the project ID where Infrastructure Manager runs.
   - <var translate="no">LOCATION</var>: the location where Infra Manager runs. See [Infrastructure Manager locations](https://docs.cloud.google.com/infrastructure-manager/docs/locations.md.txt) for the list of valid locations.
   - <var translate="no">DEPLOYMENT_ID</var>: the deployment identifier that you specify. See [Deployment name](https://docs.cloud.google.com/infrastructure-manager/docs/overview.md.txt#deployments) for details about constraints on the deployment identifier.
   - <var translate="no">REVISION_ID</var>: the revision ID representing a specific revision of a deployment. See [Deployment name](https://docs.cloud.google.com/infrastructure-manager/docs/overview.md.txt#deployments) for details about constraints on the revision identifier.
2. Preview the deletion of the resources. For details, see
   [Preview a deletion of a deployment](https://docs.cloud.google.com/infrastructure-manager/docs/preview-deployment-deletion.md.txt).

3. Delete the deployment and provisioned resources:

   ```bash
   gcloud infra-manager deployments delete projects/PROJECT_ID/locations/LOCATION/deployments/DEPLOYMENT_ID
   ```

   Replace the following:
   - <var translate="no">PROJECT_ID</var>: the project ID where Infrastructure Manager runs.
   - <var translate="no">LOCATION</var>: the location where Infra Manager runs. See [Infrastructure Manager locations](https://docs.cloud.google.com/infrastructure-manager/docs/locations.md.txt) for the list of valid locations.
   - <var translate="no">DEPLOYMENT_ID</var>: the deployment identifier that you specify. See [Deployment name](https://docs.cloud.google.com/infrastructure-manager/docs/overview.md.txt#deployments) for details about constraints on the deployment identifier.

### Keep provisioned resources

You may want to delete the deployment and keep the Google Cloud resources
that were provisioned by the deployment.

For example, you deploy a configuration from the
[Terraform blueprints and modules for Google Cloud](https://docs.cloud.google.com/docs/terraform/blueprints/terraform-blueprints.md.txt). Once it's deployed, you want to manage the resources
through the console, and not use Infrastructure as Code (IaC). In this case,
you may want to delete the deployment as you don't have use for the
deployment's metadata.

> [!NOTE]
> **Note:** After you delete the deployment, Infra Manager is not able to identify or manage the Google Cloud resources that are kept.

To delete the deployment metadata and keep the provisioned resources:

```bash
gcloud infra-manager deployments delete projects/PROJECT_ID/locations/LOCATION/deployments/DEPLOYMENT_ID \
    --delete-policy=abandon
```

Replace the following:

- <var translate="no">PROJECT_ID</var>: the project ID where Infrastructure Manager runs.
- <var translate="no">LOCATION</var>: the location where Infra Manager runs. See [Infrastructure Manager locations](https://docs.cloud.google.com/infrastructure-manager/docs/locations.md.txt) for the list of valid locations.
- <var translate="no">DEPLOYMENT_ID</var>: the deployment identifier that you specify. See [Deployment name](https://docs.cloud.google.com/infrastructure-manager/docs/overview.md.txt#deployments) for details about constraints on the deployment identifier.

## What's next

- Learn more about [Terraform with Google Cloud](https://docs.cloud.google.com/docs/terraform.md.txt).
- [View the state of a deployment](https://docs.cloud.google.com/infrastructure-manager/docs/view-deployments.md.txt).
- [View resources deployed](https://docs.cloud.google.com/infrastructure-manager/docs/view-resources.md.txt).