pg_archivecleanup is designed
to be used as an archive_cleanup_command
to clean up WAL file archives when running as a standby server
(see Section 25.2). pg_archivecleanup can also be used as a
standalone program to clean WAL file archives.

pg_archivecleanup features
include:

Written in C, so very portable and easy to install

Easy-to-modify source code, with specifically designated
sections to modify for your own needs

To configure a standby server to use pg_archivecleanup, put this into its
recovery.conf configuration file:

archive_cleanup_command = 'pg_archivecleanup archivelocation %r'

where archivelocation is the
directory from which WAL segment files should be removed.

When used within archive_cleanup_command,
all WAL files logically preceding the value of the %r argument will be removed from archivelocation. This minimizes the
number of files that need to be retained, while preserving
crash-restart capability. Use of this parameter is appropriate
if the archivelocation is a
transient staging area for this particular standby server, but
not when the
archivelocation is intended as
a long-term WAL archive area, or when multiple standby servers
are recovering from the same archive location.

The full syntax of pg_archivecleanup's command line is

pg_archivecleanup [option ... ] archivelocationrestartwalfile

When used as a standalone program all WAL files logically
preceding the restartwalfile will be
removed archivelocation. In
this mode, if you specify a .backup
file name, then only the file prefix will be used as the
restartwalfile. This allows you to
remove all WAL files archived prior to a specific base backup
without error. For example, the following example will remove
all files older than WAL file name 000000010000003700000010: