> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/reference/reference-server/pgarchivecleanup.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# pg\_archivecleanup

### pg\_archivecleanup

pg\_archivecleanup — 清理 PostgreSQL WAL 歸檔檔案

### 語法

`pg_archivecleanup` \[*`option`*...] *`archivelocation`* *`oldestkeptwalfile`*

### 說明

pg\_archivecleanup 的設計用途是作為 `archive_cleanup_command`，在以待命伺服器身分執行時清理 WAL 檔案歸檔（請參閱[第 26.2 節](https://docs.postgresql.tw/19/server-administration/high-availability/warm-standby.md)）。pg\_archivecleanup 也可以作為獨立程式，用來清理 WAL 檔案歸檔。

若要設定待命伺服器使用 pg\_archivecleanup，請將以下內容放入其 `postgresql.conf` 設定檔：

```

archive_cleanup_command = 'pg_archivecleanup archivelocation %r'
```

其中 *`archivelocation`* 是應從中移除 WAL 區段檔案的目錄。

在 [archive\_cleanup\_command](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-wal.md#GUC-ARCHIVE-CLEANUP-COMMAND) 中使用時，所有在邏輯上位於 `%r` 引數值之前的 WAL 檔案，都會從 *`archivelocation`* 中移除。這能將需要保留的檔案數量降到最低，同時保有當機後重新啟動的能力。若 *`archivelocation`* 是此特定待命伺服器的暫時中繼區，就適合使用此參數；但若 *`archivelocation`* 是要作為長期 WAL 歸檔區，或有多台待命伺服器從同一個歸檔位置進行復原，則*不*適合使用。

作為獨立程式使用時，所有在邏輯上位於 *`oldestkeptwalfile`* 之前的 WAL 檔案，都會從 *`archivelocation`* 中移除。在此模式下，若你指定 `.partial` 或 `.backup` 檔名，則只會以檔名前綴作為 *`oldestkeptwalfile`*。對 `.backup` 檔名的這種處理方式，讓你能夠移除在某個特定基礎備份之前歸檔的所有 WAL 檔案而不發生錯誤。例如，以下範例會移除所有比 WAL 檔名 `000000010000003700000010` 更舊的檔案：

```

pg_archivecleanup -d archive 000000010000003700000010.00000020.backup

pg_archivecleanup:  keep WAL file "archive/000000010000003700000010" and later
pg_archivecleanup:  removing file "archive/00000001000000370000000F"
pg_archivecleanup:  removing file "archive/00000001000000370000000E"
```

pg\_archivecleanup 假設 *`archivelocation`* 是擁有伺服器之使用者可讀寫的目錄。

### 選項

pg\_archivecleanup 接受下列命令列引數：

`-b`\
`--clean-backup-history` : 一併移除備份歷史檔案。關於備份歷史檔案的詳細資訊，請參閱[第 25.3.2 節](https://docs.postgresql.tw/19/server-administration/backup/continuous-archiving.md#backup-base-backup)。

`-d`\
`--debug` : 在 `stderr` 上印出大量除錯日誌輸出。

`-n`\
`--dry-run` : 在 `stdout` 上印出原本會被移除的檔案名稱（執行試運行）。

`-V`\
`--version` : 印出 pg\_archivecleanup 版本後結束。

`-x extension`\
`--strip-extension=extension` : 提供一個副檔名，在判斷檔案是否應刪除之前，會先從所有檔名中去除此副檔名。這通常適用於清理在儲存時經過壓縮、因而被壓縮程式加上副檔名的歸檔。例如：`-x .gz`。

`-?`\
`--help` : 顯示 pg\_archivecleanup 命令列引數的說明後結束。

### 環境

環境變數 `PG_COLOR` 指定診斷訊息是否使用色彩。可能的值為 `always`、`auto` 與 `never`。

### 注意事項

pg\_archivecleanup 設計為作為獨立工具使用時可搭配 PostgreSQL 8.0 及之後的版本，作為歸檔清理命令使用時可搭配 PostgreSQL 9.0 及之後的版本。

pg\_archivecleanup 以 C 撰寫，原始碼容易修改，並有特別標示的區段供你依自己的需求修改。

### 範例

在 Linux 或 Unix 系統上，你可能會使用：

```

archive_cleanup_command = 'pg_archivecleanup -d /mnt/standby/archive %r 2>>cleanup.log'
```

其中歸檔目錄實體上位於待命伺服器，因此 `archive_command` 是透過 NFS 存取它，但這些檔案對待命伺服器而言是本機檔案。這會：

* 在 `cleanup.log` 中產生除錯輸出
* 從歸檔目錄中移除不再需要的檔案

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/pgarchivecleanup.html)（原文版本：18.6；核對日期：2026-10-09）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/reference/reference-server/pgarchivecleanup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
