PG_ARCHIVECLEANUP(1)
π PG_ARCHIVECLEANUP β the PostgreSQL 17.11 manual page, but make it make sense
okay so bestie, gather round, because today we're talking about pg_archivecleanup β the little utility that cleans up PostgreSQL WAL archive files. yes, literally janitor energy, but make it iconic π
πΈ NAME πΈ
pg_archivecleanup β clean up PostgreSQL WAL archive files. that's it, that's the whole name. slay.
β¨ SYNOPSIS β¨
pg_archivecleanup [option...] archivelocation oldestkeptwalfile
lowkey the whole thing in one line. archivelocation is where the files live, oldestkeptwalfile is the line in the sand π«Ά
π¦ DESCRIPTION π¦
so pg_archivecleanup is designed to be used as an archive_cleanup_command β like, that's its whole main character job β to clean up WAL file archives when you're running as a standby server (see Section 26.2). but ngl it can also be a standalone program to clean WAL file archives, because it's versatile like that π
to configure a standby server to use pg_archivecleanup, drop this into its postgresql.conf configuration file:
archive_cleanup_command = 'pg_archivecleanup archivelocation %r'
where archivelocation is the directory from which WAL segment files should be removed. yeah. that's the one π―
when it's used inside archive_cleanup_command, all WAL files logically preceding the value of the %r argument get removed from archivelocation. the way this works is it minimizes the number of files you need to retain while still preserving crash-restart capability β best of both worlds, no cap.
β οΈ but listen: using this parameter is appropriate if thearchivelocationis a transient staging area for this particular standby server. it is not appropriate when thearchivelocationis intended as a long-term WAL archive area, or when multiple standby servers are recovering from the same archive location. read that again pls, it matters π
when used as a standalone program, all WAL files logically preceding the oldestkeptwalfile get removed from archivelocation. and in this mode, if you specify a .partial or .backup file name, then only the file prefix will be used as the oldestkeptwalfile. this treatment of .backup file name allows you to remove all WAL files archived prior to a specific base backup without error β which is genuinely so convenient, obsessed π
for example, the following example will remove all files older than WAL file name 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"
see how it kept the one and ate the rest? periodt π
also, pg_archivecleanup assumes that archivelocation is a directory readable and writable by the server-owning user. like, it's not gonna fight you on permissions, it just assumes. so maybe double check that before you come crying π
π OPTIONS π
pg_archivecleanup accepts the following command-line arguments:
-b,--clean-backup-historyβ remove backup history files as well. see Section 25.3.2 for the details about backup history files if you wanna go deeper π§-d,--debugβ prints lots of debug logging output onstderr. like, a lot. it's giving over-sharer and honestly? we love that for us.-n,--dry-runβ prints the names of the files that would have been removed onstdout. that's right, it performs a dry run β nothing actually gets deleted, it's just peeking π-V,--versionβ prints thepg_archivecleanupversion and exits. quick vibe check, then it's out π-x extension,--strip-extension=extensionβ provides an extension that will be stripped from all file names before deciding if they should be deleted. this is typically useful for cleaning up archives that have been compressed during storage, and therefore have had an extension added by the compression program. for example:-x .gz. so smart honestly π-?,--helpβ shows help aboutpg_archivecleanupcommand line arguments, and exits. your friendly little map β¨
π ENVIRONMENT π
the environment variable PG_COLOR specifies whether to use color in diagnostic messages. possible values are always, auto and never. pick your aesthetic π
π NOTES π
pg_archivecleanup is designed to work with PostgreSQL 8.0 and later when used as a standalone utility, or with PostgreSQL 9.0 and later when used as an archive cleanup command. old but still relevant, we stan a legacy icon π
pg_archivecleanup is written in C and has an easy-to-modify source code, with specifically designated sections to modify for your own needs. so if you wanna remix it, the blueprint is right there π οΈ
π₯ EXAMPLES π₯
on Linux or Unix systems, you might use:
archive_cleanup_command = 'pg_archivecleanup -d /mnt/standby/archive %r 2>>cleanup.log'
where the archive directory is physically located on the standby server, so that the archive_command is accessing it across NFS β but the files are local to the standby. iykyk πΈ
this will:
- produce debugging output in
cleanup.log - remove no-longer-needed files from the archive directory
two things. both important. both slaying π
and that's the whole page, bestie!! this was PostgreSQL 17.11 documentation, dated 2026 β go forth and clean those archives responsibly, mwah π«Άπβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17-contrib 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17-contrib 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17-contrib 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17-contrib 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms