# 卸载与清理

> 分别移除 Linux 与 macOS 虚拟机，再清理集成、镜像、网络和程序。

---

LLMS 索引： [llms.txt](/v1.5/zh/llms.txt)

---

本页用于移除 Barn 创建的部分或全部内容。Linux 实验环境和 macOS 虚拟机分开管理，`barn purge` 不会动任何 Mac 虚拟机。清理命令都依赖 `barn`，所以请把卸载程序留到最后一步。

## 选择清理范围 {#choose-what-to-remove}

| 要移除 | 执行 | 保留下来的内容 |
|---|---|---|
| 暂不删除，只停止虚拟机 | `barn stop`；`barn mac stop --all` | 全部保留，包括磁盘 |
| 单个 Linux 节点 | `barn destroy <node>` | 其他节点、该节点的持久数据盘、密钥、镜像、宿主网络 |
| Linux 实验环境 | `barn destroy` | 持久数据盘、部署密钥、镜像缓存、宿主网络 |
| Linux 实验环境及其持久数据盘和密钥 | `barn purge`（立即删除；`--ask` 先确认） | 镜像缓存、宿主网络 |
| 指定的 Mac 虚拟机 | `barn mac destroy <name...>`（需要确认） | 其他 Mac 虚拟机、macOS 镜像缓存 |
| Barn 的 SSH、`/etc/hosts` 与校时集成 | [第 2 步](#full-teardown-steps) | 你自己的 `~/.ssh/config` 和 `/etc/hosts` 条目 |
| 镜像缓存 | `barn image prune --yes`；`barn mac image prune --installers --yes` | 仍被虚拟机使用的镜像，以及默认 macOS 基础镜像 |
| Linux 宿主网络 | `barn network uninstall --yes`（删除全部 Linux 虚拟机之后） | — |
| Barn 程序本身 | [第 5 步](#full-teardown-steps) | `~/.barn`，直到你手动删除 |
{.wide .expand}

删除之前，先用 `barn status`（Linux）和 `barn mac ls`（Mac）核对名称，并把要保留的文件拷到宿主上。各命令对数据盘的具体影响见[各操作的保留规则](../storage/#what-survives-each-operation)。

## 完整清理步骤 {#full-teardown-steps}

1. **销毁 Linux 与 Mac 虚拟机**

   丢弃整套 Linux 实验环境，包括持久数据盘和部署密钥：

   ```bash
   barn purge
   ```

   该命令立即删除。想先查看虚拟机列表和清理范围，改用 `barn purge --ask`，输入 `y`/`yes` 确认或 `n`/`no` 取消；`purge` 不接受 `--yes`、`-y` 或 `--force`。然后列出并按名称销毁 macOS 虚拟机：

   ```bash
   barn mac ls
   barn mac destroy mac1 dev
   ```

2. **移除 SSH、`/etc/hosts` 与定时校时集成**

   销毁整套 Linux 部署时已经移除了默认的 `~/.ssh/barn_config` 集成。其余自行安装过的集成需要单独移除：自定义名称的 SSH 片段、`/etc/hosts` 条目、Mac SSH 别名，以及校时用户定时任务：

   ```bash
   barn ssh-config --remove --name lab
   barn hosts uninstall --json
   barn hosts uninstall --yes
   barn mac ssh-config --remove
   barn time auto disable
   ```

   每条命令只删除 Barn 写入的配置块。

3. **清理镜像缓存**

   先预览，再删除缓存的 Linux 与 macOS 镜像：

   ```bash
   barn image prune --dry-run
   barn image prune --yes
   barn mac image prune --installers
   barn mac image prune --installers --yes
   ```

4. **卸载 Linux 宿主网络**

   只有在没有 Linux 虚拟机运行时，才能卸载共享的 Linux 宿主网络。Mac 虚拟机的网络会在虚拟机停止时自动释放。

   ```bash
   barn network uninstall --json
   barn network uninstall --yes
   ```

   宿主使用 systemd-networkd 时，如果还有其他网络接口，或 Barn 无法确认自己是唯一使用者，卸载会保留 networkd 的全局单元状态；计划中会报告 `preserve_networkd`，执行结果在 `checks.networkd` 中说明原因。

5. **卸载 Barn 程序与剩余文件**

   通过包管理器安装的 Barn，直接卸载对应软件包：

   ```bash {tab="Homebrew" group="uninstall" value="brew"}
   brew uninstall barn
   ```

   ```bash {tab="Debian / Ubuntu" group="uninstall" value="deb"}
   sudo apt remove barn
   ```

   ```bash {tab="RHEL / Fedora" group="uninstall" value="rpm"}
   sudo dnf remove barn
   ```

   - **用户态 `install.sh`**：删除 `~/.local/bin` 下的 `barn`、`barn-hosts-helper`、`.barn-current`、`.barn-releases/` 和 `.barn-install.lock`，并去掉 Shell 配置文件中的 `PATH` 设置。
   - **手动解压的发行包或源码构建**：停止全部 Mac 客机后，删除解压出的发行目录、`bin/mac` 或复制出去的 `Barn.app`（`0.9.0` 中为 `Barn Mac.app`），以及 `~/Library/Preferences/com.pgsty.barn.plist`（`0.9.0` 中为 `io.pgsty.barn.mac-runner.plist`）。
   - **手动安装的 hosts helper**：如果手动安装过 `/opt/barn/libexec/barn-hosts-helper`，且本机没有其他用户使用 Barn，在执行 `barn hosts uninstall --yes` 之后将其删除。
{.steps}

> [!WARNING]
> **先销毁全部 Linux 和 Mac 虚拟机，再删除 `~/.barn`（或自定义的 `BARN_HOME`）。** 该目录同时存放两类虚拟机的状态、密钥、持久数据盘和基础镜像。如果仍有虚拟机在运行，或某次 `destroy` 失败，请先用 `barn status` / `barn mac ls` 处理，再删除目录。
