# 9.3.pkg-deinstall、pkg-pre-deinstall 和 pkg-post-deinstall（卸载时执行的脚本文件）

这些脚本在包被移除时执行。

**pkg-deinstall** 脚本由 `pkg delete` 执行两次。第一次是 `${SH} pkg-deinstall ${PKGNAME} DEINSTALL`，在 Port 被卸载之前，第二次是 `${SH} pkg-deinstall ${PKGNAME} POST-DEINSTALL`，在 Port 被卸载之后。可以测试 `$2` 来确定脚本正在以哪种模式运行。`PKG_PREFIX` 环境变量设置为包的安装目录。

如果使用 **pkg-pre-deinstall** 或 **pkg-post-deinstall**，脚本仅运行一次（在卸载包之前或之后），并且带有一个参数 `${PKGNAME}`。如果使用 **pkg-pre-deinstall.lua** 或 **pkg-post-deinstall.lua**，则会运行 Lua 脚本而不是 shell 脚本。通过 `pkg` 运行的 Lua 脚本提供了一些扩展功能和一些限制，这些都在 [pkg-lua-script(5)](https://man.freebsd.org/cgi/man.cgi?query=pkg-lua-script\&sektion=5\&format=html) 中进行了说明。

> **注意**
>
> 推荐使用 **pkg-pre-deinstall**（或 **pkg-pre-deinstall.lua**）和 **pkg-post-deinstall**（或 **pkg-post-deinstall.lua**），而不是使用 **pkg-deinstall**。

这些脚本会自动添加到打包列表中。

> **重要**
>
> 这些脚本用于简化包卸载后的清理工作。*不能* 滥用它们来启动服务、停止服务或运行任何其他会修改当前运行系统的命令。


---

# 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://porters-handbook.bsdcn.org/di-9-zhang-pkg/9.3.pkgdeinstall-xie-zai-shi-zhi-hang-de-jiao-ben-wen-jian.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.
