> For the complete documentation index, see [llms.txt](https://wiki.datadike.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.datadike.com/sonar/deploy-operate/remove-agents.md).

# Remove agents

Remove the SONAR agent from the device action menu, uninstall it locally on Windows, Linux or macOS, or archive a device to keep its history.

## What this is and when to use it

Removing an agent uninstalls SONAR from the device and takes the device out of the console. Use it when a device is retired, replaced or leaves your management. There are three ways, with different results:

| Action                                 | Uninstalls from the device     | Device record in the console                                     |
| -------------------------------------- | ------------------------------ | ---------------------------------------------------------------- |
| **Remove Agent** (device action menu)  | Yes, when the device is online | Deleted, with its data                                           |
| **Archive Agent** (device action menu) | Yes, when the device is online | Kept, with its history, marked as archived                       |
| Local uninstall on the device          | Yes                            | Kept; the device goes offline until you remove it in the console |

## Before you start

* **Remove Agent** needs **Uninstall Agents** in **Admin Center › Roles**. **Archive Agent** needs **Edit Agent**. Both only work on devices of sites your role gives you access to.
* Decide whether you still need the device's history in the console. **Remove Agent** deletes it; **Archive Agent** keeps it.
* For a local uninstall, you need administrator rights on Windows or `root` on Linux and macOS.

## Steps

### Remove an agent from the console

1. In **Dashboard › Sysadmin**, select the device.
2. Open the device action menu (**Other actions**, in the details panel header) and choose **Remove Agent**.
3. SONAR first checks whether the device answers.
   * If it does not, the **Agent offline** dialog says *… cannot be contacted. Would you like to continue with the uninstall? If so, the agent will need to be manually uninstalled from the computer.* Click **Yes** to continue or **No** to stop.
4. In **Remove \<hostname>**, type the **Reason**. It is required and is stored in the audit trail.
5. Click **Remove Agent**.

### Archive an agent

1. Select the device and open the device action menu.
2. Choose **Archive Agent**.
3. In **Archive Agent**, type `yes` to confirm, and click **Archive**.

**Archive Agent** uninstalls the agent from the device in the same way as **Remove Agent**, but the device record and its history stay in the console. SONAR raises no alerts for an archived device, and archived devices are not counted among online, offline or overdue devices.

**Unarchive Agent**, in the same menu, clears the archived mark. It does not reinstall anything: to manage the device again, install the agent on it. It registers as a new device record.

### Uninstall locally on Windows

In an elevated Command Prompt:

```
"C:\Program Files\Sonar\uninstall.exe" /S
```

For a 32-bit agent on 64-bit Windows the uninstaller is `C:\Program Files (x86)\Sonar\uninstall.exe`. The agent can also be removed as **Sonar** from the installed programs list in Windows.

### Uninstall locally on Linux

Run the Linux install script from **Agents › Install Agent** with the `uninstall` argument, as root:

```bash
sudo bash ./sonar-<client>-<site>-<type>.sh uninstall
```

Any Linux script generated by the console works for this, whatever its client, site or expiry. The script deletes itself when it finishes.

### Uninstall locally on macOS

As an administrator, in Terminal:

```bash
sudo /opt/sonarmesh/meshagent -fulluninstall
sudo launchctl bootout system /Library/LaunchDaemons/sonaragent.plist
sudo rm -rf /opt/sonarmesh /opt/sonaragent /usr/local/mesh_services
sudo rm -f /etc/sonaragent.json /Library/LaunchDaemons/sonaragent.plist
```

## What you should see

**Remove Agent** ends with *\<hostname> will now be uninstalled.* and the device leaves the device list. **Archive Agent** ends with *Archived was enabled on \<hostname>*.

What the uninstall removes from the device:

| Platform | Removed                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Windows  | The `sonarrmm` service; the productivity module service `SonarProductivityService`; the remote access agent; the `HKLM\SOFTWARE\Sonar` configuration; `C:\ProgramData\Sonar\` and the scheduled tasks created by SONAR; the `C:\Program Files\Sonar\` and `C:\Program Files\Mesh Agent\` folders; the **Sonar** entry in the installed programs list. The Windows Update policy set at installation is reverted, so Windows installs updates automatically again |
| Linux    | `sonaragent.service` and its unit file, `/usr/local/bin/sonaragent`, `/opt/sonaragent/`, `/etc/sonaragent.json`; the remote access agent, `meshagent.service` and `/opt/sonarmesh/`                                                                                                                                                                                                                                                                              |
| macOS    | The `sonaragent` launch daemon and its plist, `/opt/sonaragent/`, `/etc/sonaragent.json`; the remote access agent and `/opt/sonarmesh/`                                                                                                                                                                                                                                                                                                                          |

With **Remove Agent** and **Archive Agent**, the device is also removed from the remote access service, so remote control is no longer offered for it.

## How to verify it worked

1. **Console, Remove Agent:** the device no longer appears in **Dashboard › Sysadmin** when you search for its host name. In **Reports & Audits › Audits**, a **Delete** entry reads *\<user> deleted agent \<hostname> - \<reason>*.
2. **Console, Archive Agent:** the device action menu of the device now offers **Unarchive Agent**.
3. **Windows device:** `sc query sonarrmm` returns *The specified service does not exist as an installed service*, and `C:\Program Files\Sonar\` is gone.
4. **Linux device:** `systemctl status sonaragent` reports that the unit could not be found.
5. **macOS device:** `sudo launchctl print system/sonaragent` reports that the service could not be found.

## Common errors

| Symptom                                                                                 | Cause                                                                                             | Fix                                                                                                                               |
| --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| The **Agent offline** dialog appears                                                    | The device is switched off or cannot reach SONAR, so the uninstall command cannot be delivered    | Click **Yes** to remove the record, then uninstall locally on the device; or click **No** and try again when the device is online |
| The device was removed in the console but the agent is still on the computer            | The device was offline when it was removed                                                        | Uninstall locally, using the commands above                                                                                       |
| A device uninstalled locally stays in the list as offline                               | A local uninstall does not touch the console                                                      | Remove it with **Remove Agent** and answer **Yes** in **Agent offline**                                                           |
| **Remove Agent** stays disabled in **Remove \<hostname>**                               | The **Reason** is empty                                                                           | Type a reason                                                                                                                     |
| **Remove Agent** or **Archive Agent** shows *You don't have permission for this action* | Your role lacks **Uninstall Agents** or **Edit Agent**                                            | Ask for the permission                                                                                                            |
| After reinstalling, the device appears twice                                            | The old record was kept (archived, or uninstalled locally) and the reinstall registered a new one | Remove the old record with **Remove Agent**                                                                                       |

## Related pages

* [The device list](https://wiki.datadike.com/sonar/administration/devices/device-list.md)
* [Install the agent on Windows](https://wiki.datadike.com/sonar/deploy-operate/install-agent-windows.md)
* [Install the agent on Linux](https://wiki.datadike.com/sonar/deploy-operate/install-agent-linux.md)
* [Install the agent on macOS](https://wiki.datadike.com/sonar/deploy-operate/install-agent-macos.md)
* [Audit trail](https://wiki.datadike.com/sonar/administration/audit/audit-trail.md)
