# Force Remove the Host Role From DB

## Problem

Unable to remove a host role from a specific host via the Platform9 UI, `pcdctl` and `curl` command. The UI shows an `RoleUpdateConflict` error when attempting to deauthorize and decommission the host.

```bash
$  curl -s -X DELETE -H "X-Auth-Token: $token" https://<PCD_FQDN>/resmgr/v2/hosts/<HOST-UUID>/roles/<ROLE_NAME> -vvv
...
"message":"RoleUpdateConflict: Role cannot be updated in the current state: Cannot remove role [ROLE_NAME] from host [HOST_UUID] in the current state".
```

## Environment

* Self-Hosted Private Cloud Director Virtualization&#x20;
* Component: ResMgr

## Cause

The host role remained in a `not-applied` state in the backend database, which blocked further updates or role removal actions through the UI. This inconsistency prevented the host from being deauthorized or decommissioned.

## Diagnostics

{% hint style="info" %}
These steps are applicable only for Self-hosted Private Cloud Director product
{% endhint %}

To check the role state:

1. Login to the resmgr DB:

```bash
$ NS=<NAMESPACE>

$ DB_PASS=$(kubectl exec -i -n "$NS" $(kubectl get po -n "$NS" -l du-app=resmgr -o jsonpath="{.items[0].metadata.name}") -c resmgr -- /bin/bash -c "DB=\$(consul-dump-yaml --start-key customers/\$CUSTOMER_ID/regions/\$REGION_ID/db | awk '/dbserver:/ {print \$2}'); consul-dump-yaml --start-key customers/\$CUSTOMER_ID/dbservers/\$DB | awk '/admin_pass:/ {print \$2}'")

$ IP=$(kubectl get pods -n $NS  $(kubectl get po -n $NS -l du-app=mysql -o jsonpath="{.items[0].metadata.name}") --template '{{.status.podIP}}')

$ kubectl exec -it -n "$NS" $(kubectl get po -n "$NS" -l du-app=resmgr -o jsonpath="{.items[0].metadata.name}") -c resmgr -- /bin/bash -c "mysql -u root -h $IP -p$DB_PASS"
```

2. Check the role state:

<pre class="language-bash"><code class="lang-bash">MySQL [resmgr]> "SELECT * FROM host_role_map WHERE res_id = '&#x3C;HOST_UUID>';"

+----------------+-------------+---------------+
| res_id         | rolename    | current_state |
+----------------+-------------+---------------+
<strong>| [HOST_UUID]    | [ROLE_NAME] |  not-applied  |
</strong>+----------------+-------------+---------------+
</code></pre>

The `not-applied` state indicates that the role was not fully or correctly applied, resulting in a conflict when attempting UI-based role changes.

## Resolution

{% hint style="danger" %}
**Caution:** The following steps involve direct database manipulation and should only be performed at most care.
{% endhint %}

1. Delete the role from the backend database:

```bash
MySQL [resmgr]> "DELETE FROM host_role_map WHERE res_id='<HOST_UUID>';"
```

2. Verify that the role mapping has been removed:

```bash
MySQL [resmgr]> "SELECT * FROM host_role_map WHERE res_id = '<HOST_UUID>';"

## Expected output: Empty set
```

{% hint style="warning" %}
**Caution:** The following steps only if need to delete the host as well.
{% endhint %}

3. Optionally, delete the host from the database if needed:

```bash
MySQL [resmgr]> "DELETE FROM hosts WHERE id='<HOST_UUID>';"
```

4. Purge PF9 packages (if host is being decommissioned):

```bash
$ sudo dpkg --purge --force-all pf9-hostagent
$ sudo dpkg --purge --force-all pf9-comms
$ sudo dpkg --purge --force-all pf9-neutron-ovn-controller
$ sudo dpkg --purge --force-all pf9-ostackhost
```

5. Clean up PF9 services and directories:

```bash
$ sudo systemctl stop 'pf9-*.service'
$ sudo systemctl disable 'pf9-*.service'
$ sudo systemctl daemon-reexec
$ sudo systemctl daemon-reload

$ rm -rf /etc/pf9 /opt/pf9 /var/log/pf9 /var/opt/pf9
```

## Validation

* Confirm that the role mapping associated with the host has been removed
* The host is no longer listed in the UI under Infrastructure > Hosts.
* If re-onboarding, reauthorize the host and apply roles cleanly.
* Review logs (`/var/log/pf9/hostagent.log`, `/var/log/pf9/comms/`) to ensure role application is successful.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://platform9.com/kb/pcd/generic/force-remove-the-host-role-from-db-for-self-hosted-pcd.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
