PG_ARCHIVECLEANUP(1)

πŸ“¦ version 17.11, in opensuse-leap 16.0

πŸ’… 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 the archivelocation is a transient staging area for this particular standby server. it is not appropriate when the archivelocation is 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 on stderr. 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 on stdout. that's right, it performs a dry run β€” nothing actually gets deleted, it's just peeking πŸ‘€
  • -V, --version β€” prints the pg_archivecleanup version 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 about pg_archivecleanup command 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 πŸ«ΆπŸ’–βœ¨