# Uninstall an installer installation

Remove an installation that the AWS or Google Cloud installer made, keep or delete its data, and delete what stays.

The installer that made the installation also removes it. On AWS, you delete the CloudFormation stack. On Google Cloud, you run `teardown.sh` in Cloud Shell. By default, both keep the data of Platforma, so a mistake does not delete the results and the projects. The bucket of the primary storage (The bucket (Amazon S3 or Google Cloud Storage) or folder where Platforma keeps all results and uploaded files.), the workspace (The storage where the Backend and all jobs keep the working files of running jobs. The Backend and the jobs use it at the same time. In Kubernetes, it is a network file system; in Single Server mode, it is a folder on the server.) and the database disk stay until you delete them yourself. For the other installation paths (A set of tools and steps that installs Platforma. There are five installation paths: the AWS installer, the Google Cloud installer, the Terraform modules, the Helm chart and Single Server mode.), see [Uninstall Platforma](/maintain-uninstalling/).

## Before you start

1. **Make a backup** of the database, and copy it out of the installation: see [Make a backup now](/maintain-backup-and-restore/#make-a-backup-now). The backup files are in the bucket of the primary storage. You can delete this bucket later.

2. **Keep a copy of the master secret** in the secret store of the company. Without it, all users sign in (To show the Backend who you are, with a sign-in provider or a single-user token. The verb is "sign in"; the noun and the adjective are "sign-in".) again after a reinstall, and the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) cannot read the storage keys that a kept database stores for a storage that is no longer in the settings. See [What to keep](/maintain-backup-and-restore/#what-to-keep). The AWS stack and the default Google Cloud teardown keep the master secret; keep a copy in case someone deletes it. The Google Cloud teardown that deletes all data also deletes the master secret in Secret Manager: then your copy is the only one.

   #### Variant: AWS installer

   ```sh
   aws ssm get-parameter --name /<CLUSTER_NAME>/platforma/master-secret \
     --with-decryption --query Parameter.Value --output text > master-secret.txt
   ```

   #### Variant: Google Cloud installer

   ```sh
   gcloud secrets versions access latest --project=<PROJECT_ID> \
     --secret=<DEPLOYMENT_NAME>-cluster-platforma-master-secret > master-secret.txt
   ```

3. **Have the rights.**

   #### Variant: AWS installer

   The person who deletes the stack needs the three installer policies: see [Permissions](/plan-permissions/). Some of their statements also let you delete the parts that stay after the uninstall: `LeftLogGroups`, `KeptVolumes`, `KeptVolumesRead` and `KeptMasterSecret`.

   #### Variant: Google Cloud installer

   The person who runs `teardown.sh` needs the roles of the installation and the two `container` roles: see [Permissions](/plan-permissions/). Each teardown needs the role `roles/config.admin` (Infrastructure Manager Admin) to delete the deployments. The default teardown keeps the data, and for this it needs:

   - from `roles/config.admin`, on the project or on the deployment `<DEPLOYMENT_NAME>-infra`, the permissions `config.deployments.lock`, `getLock`, `getState`, `updateState` and `unlock`. With them, the script takes the kept data out of the Terraform state of the deployment;
   - the permission `compute.disks.setLabels` on the project, for example in the role `roles/compute.storageAdmin`, to put labels on the kept database disk.

   In the default mode, `teardown.sh` checks these rights before it changes anything. When one is missing, it stops and shows the command that grants the role. The teardown that deletes all data does not check them first, and it does not need the state permissions or `compute.disks.setLabels`. It deletes the Secret Manager secrets of the installation: for this, it uses the role `roles/secretmanager.admin`, which the installation also needs.

4. **Google Cloud: use the `teardown.sh` of the release of the last `install.sh` run.** After an update, this is the release tag of the update. In Cloud Shell, the folder `platforma-helm` of that run has it; `git -C platforma-helm describe --tags` shows the release tag.

## Remove the installation

#### Variant: AWS installer

The stack always keeps the data of Platforma. There is no option to delete the data with the stack: you delete the kept parts later, when you do not need them.

1. **Write down the bucket.** In the CloudFormation console, open the stack and note the output `S3BucketOutput`: the name of the bucket of the primary storage. The outputs go away with the stack.

2. **Delete the stack.** Select the stack and choose **Delete**. The stack removes these parts: Platforma, the load balancer, the DNS records of the domain, the cluster, the network and the roles. It keeps these parts:

   - the bucket, with the results of all projects and the database backups;
   - the workspace;
   - the database volume, with all projects, and the logs volume;
   - the master secret.

   Result: after about 25 minutes, the status of the stack is `DELETE_COMPLETE`.

   The log of the delete names the kept volumes. To see their IDs:

   ```sh
   aws logs filter-log-events --log-group-name /aws/codebuild/<CLUSTER_NAME>-helm-deployer \
     --filter-pattern '"KEPT EBS volume ("' --query 'events[].message' --output text
   ```

#### Variant: Google Cloud installer

`teardown.sh` has two modes:

- **Keep the data** (the default). The script keeps the bucket of the primary storage, the Filestore instance of the workspace with its VPC network, the database disk, and the secrets of the installation in Secret Manager. It deletes all other parts, also the logs disk.
- **Delete all data**: `DELETE_ALL_DATA=true` or the flag `--delete-all-data`. The script also deletes the bucket, with the results of all projects and the database backups. It deletes the workspace, the database disk with all projects, the VPC network, and the secrets in Secret Manager, also the master secret. You cannot get them back.

1. **Open the folder of the installer and load the answers.** In Cloud Shell, go to the folder of the release of the last `install.sh` run, and load the answers that you saved at the installation. The script uses `PROJECT_ID`, `DEPLOYMENT_NAME` and `IM_LOCATION`. Their defaults are the project of `gcloud`, `platforma` and `europe-west1`.

   ```sh
   cd platforma-helm/infrastructure/gcp/cloudshell
   source platforma-answers.sh
   ```

2. **Run the teardown** in the mode that you need:

   ```sh
   # Keep the data
   bash teardown.sh


   # Delete all data: the results of all projects, the database backups, all projects
   # and the secrets in Secret Manager, also the master secret
   DELETE_ALL_DATA=true bash teardown.sh
   ```

   The script shows what it deletes and what it keeps. Then it asks you to type `delete <DEPLOYMENT_NAME> from <PROJECT_ID>`. The script stops the Backend and deletes the deployment `<DEPLOYMENT_NAME>-platforma`. Then it deletes the deployment `<DEPLOYMENT_NAME>-infra`: the cluster, the network of the nodes, Cloud NAT, the service accounts, the certificate and the DNS record. Each deployment takes about 10 minutes.

   Result: the script shows `Teardown complete: the customer data is kept`, with the names of the kept parts and the commands that delete them. In the other mode, it shows `Teardown complete: all data is deleted`. Save this output.

   If Cloud Shell disconnects, run the script again. Do not delete the deployments in Infrastructure Manager directly. Such a delete stops on the disks of Platforma, and it deletes the workspace.

## What stays and how to delete it

The kept parts cost money until you delete them. Delete them only after you copied the backup and the results that you need. The bucket holds the results of all projects and the database backups. The database volume or disk holds all projects.

#### Variant: AWS installer

These parts stay after the stack is deleted:

- **The S3 bucket of the primary storage** (`S3BucketOutput`), with the results of all projects and the database backups.
- **The EFS file system of the workspace**, with the name `<CLUSTER_NAME>-workspace`.
- **The EBS volumes of the database and the logs**. Their StorageClass keeps them also when their claims or the namespace are deleted before the stack. The database volume holds all projects.
- **The master secret** in Parameter Store, `/<CLUSTER_NAME>/platforma/master-secret`. A new stack with the same cluster name uses it again.
- **The log groups of the stack**: `/aws/codebuild/<CLUSTER_NAME>-helm-deployer`, `/aws/codebuild/<CLUSTER_NAME>-platforma-deployer`, `/aws/lambda/<CLUSTER_NAME>-helm-trigger`, and with GPU nodes (`EnableGpu`) also `/aws/lambda/<CLUSTER_NAME>-gpu-subnet-resolver`.
- **The DNS validation record of the certificate**: a `CNAME` record in the hosted zone of the domain. Its name starts with `_` and its value ends with `acm-validations.aws.`

To delete them with the AWS CLI, replace the values in angle brackets:

```sh
# Empty and delete the bucket. This deletes the results of all projects and the database backups.
aws s3 rb s3://<BUCKET_NAME> --force


# Find and delete the volumes of the database and the logs. This deletes all projects.
aws ec2 describe-volumes \
  --filters Name=status,Values=available Name=tag:ebs.csi.aws.com/cluster-name,Values=<CLUSTER_NAME> \
  --query "Volumes[].[VolumeId,Size,Tags[?Key=='kubernetes.io/created-for/pvc/name'].Value|[0]]" \
  --output table
aws ec2 delete-volume --volume-id <VOLUME_ID>


# Delete the master secret, only together with the database volume.
aws ssm delete-parameter --name /<CLUSTER_NAME>/platforma/master-secret


# Find and delete the file system of the workspace.
aws efs describe-file-systems \
  --query "FileSystems[?Name=='<CLUSTER_NAME>-workspace'].FileSystemId" --output text
aws efs delete-file-system --file-system-id <FILE_SYSTEM_ID>


# Delete the log groups. Without GPU nodes, the last one does not exist.
for g in /aws/codebuild/<CLUSTER_NAME>-helm-deployer /aws/codebuild/<CLUSTER_NAME>-platforma-deployer \
         /aws/lambda/<CLUSTER_NAME>-helm-trigger /aws/lambda/<CLUSTER_NAME>-gpu-subnet-resolver; do
  aws logs delete-log-group --log-group-name "$g"
done
```

To delete the validation record, open the hosted zone in the Route 53 console, select the `CNAME` record of the certificate and choose **Delete record**.

#### Variant: Google Cloud installer

After a teardown that keeps the data, these parts stay. `<name>` is `DEPLOYMENT_NAME`:

- **The bucket of the primary storage**, `platforma-<name>-cluster-<suffix>`, with the results of all projects and the database backups.
- **The Filestore instance of the workspace**, `<name>-cluster-workspace`.
- **The database disk**, with all projects. It has the labels `platforma-installation=<name>-cluster` and `platforma-component=database`.
- **The VPC network** `<prefix>-vpc`, with its private service access range `<prefix>-vpc-psa` and the peering: the Filestore instance needs them. `<prefix>` is `RESOURCE_NAME_PREFIX`, by default `platforma`.
- **The secrets of the installation in Secret Manager**, `<name>-cluster-*`: the master secret, the administrator password, the license key, and the sign-in secrets and data library (A folder or bucket with input data of the company. Users select files from it in the Desktop App. Platforma only reads it.) keys that you gave. For the full list, see [How secrets are handled](/security-secrets/#how-secrets-are-handled). A new installation with the same `DEPLOYMENT_NAME` uses the master secret and the administrator password again. Delete the master secret only together with the database disk.

After each teardown, also after the one that deletes all data, these parts stay:

- the service account `platforma-im-deployer`, with the Owner role on the project. All installations in the project share it: keep it while another installation in the project uses it;
- the Cloud DNS zone of the domain, which the installer did not make.

To delete the kept data later, run the teardown again in the mode that deletes all data. Use the same folder and the same answers. The script finds the kept parts by their names and labels, deletes them and checks that they are gone. This deletes the results of all projects, the database backups, all projects and the secrets in Secret Manager, also the master secret. It needs these roles, or Owner: `roles/file.editor`, `roles/compute.storageAdmin`, `roles/compute.networkAdmin`, `roles/servicenetworking.networksAdmin` and `roles/secretmanager.admin`.

```sh
source platforma-answers.sh
DELETE_ALL_DATA=true bash teardown.sh
```

Or delete the parts one by one, with the commands that the teardown showed at its end:

```sh
gcloud storage rm -r gs://<BUCKET_NAME> --project=<PROJECT_ID>
gcloud filestore instances delete <name>-cluster-workspace --location=<ZONE> --project=<PROJECT_ID>
gcloud compute disks list --project=<PROJECT_ID> \
  --filter="labels.platforma-installation=<name>-cluster AND labels.platforma-component=database"
gcloud compute disks delete <DISK_NAME> --zone=<ZONE> --project=<PROJECT_ID>


# After the Filestore instance is deleted:
gcloud services vpc-peerings delete --network=<prefix>-vpc \
  --service=servicenetworking.googleapis.com --project=<PROJECT_ID>
gcloud compute addresses delete <prefix>-vpc-psa --global --project=<PROJECT_ID>
gcloud compute networks delete <prefix>-vpc --project=<PROJECT_ID>


# Find and delete the secrets of the installation. Delete the master secret only together
# with the database disk.
gcloud secrets list --project=<PROJECT_ID> --filter="labels.platforma-installation=<name>-cluster"
gcloud secrets delete <SECRET_NAME> --project=<PROJECT_ID>
```

To delete the service account of the installer:

```sh
gcloud projects remove-iam-policy-binding <PROJECT_ID> \
  --member=serviceAccount:platforma-im-deployer@<PROJECT_ID>.iam.gserviceaccount.com \
  --role=roles/owner --condition=None
gcloud iam service-accounts delete platforma-im-deployer@<PROJECT_ID>.iam.gserviceaccount.com \
  --project=<PROJECT_ID>
```

## Install again with the same names

The installers do not attach kept data to a new installation: a new installation starts with new, empty storage. To use the kept bucket, workspace and database disk again, install with the Terraform modules (The Terraform code that MiLaboratories provides to install Platforma. The cloud-specific Terraform modules make the cluster and its cloud parts in AWS or Google Cloud; the cloud-agnostic Terraform modules install Platforma into the cluster.): see [Kept data and reuse](/configuration-storage/#kept-data-and-reuse).

#### Variant: AWS installer

A new stack with the same cluster name uses the kept master secret. It makes a new bucket, a new file system and new volumes, and the kept ones stay until you delete them.

#### Variant: Google Cloud installer

A new `install.sh` run with the same `DEPLOYMENT_NAME` checks for kept data with its names: a bucket, a Filestore instance or a VPC network. When it finds one, it stops before it makes anything. Then do one of these, and run `install.sh` again:

- delete the kept data: `DELETE_ALL_DATA=true bash teardown.sh` with the old `DEPLOYMENT_NAME` (see [What stays and how to delete it](/maintain-uninstalling-cloud-installer/#what-stays-and-how-to-delete-it));
- install with a new `DEPLOYMENT_NAME`. When the VPC network is kept, also set a new `RESOURCE_NAME_PREFIX`: the name of the network does not depend on `DEPLOYMENT_NAME`.

A kept database disk or kept secrets in Secret Manager do not stop the installer: it makes a new disk. With the same `DEPLOYMENT_NAME`, the installer uses the kept master secret and the kept administrator password again.

## Next

- [Uninstall Platforma](/maintain-uninstalling/)
- [Back up and restore](/maintain-backup-and-restore/)

Source: https://docs.platforma.bio/maintain-uninstalling-cloud-installer/
