skip to content

In WAL-G for PostgreSQL, what does wal-g delete retain FULL 7 --confirm keep and delete, and when can delete retain remove the wrong backups?

level: middleimportance: should knowfreq 42%

answer

  1. a dry run unless confirmed
  2. N counts deltas unless FULL
  3. WAL older than the oldest kept backup
  4. permanent backups are skipped
  5. ordering by name after a timeline reset

basics

~20 s

It keeps the seven newest full backups with their deltas and deletes older backups plus the WAL before the oldest one kept. Without --confirm it is a dry run; after pg_upgrade name-based ordering can delete the new backups.

solid answer

~50 s

`wal-g delete` is a dry run by default and only deletes with `--confirm`. `retain FULL 7` keeps the seven most recent full backups and every delta that depends on them, then removes older backups and the WAL older than the oldest backup kept, so the PITR window starts at that backup. A plain `retain 7` counts deltas too and fails if the seventh is a delta; `retain FIND_FULL 7` keeps back to the full that seventh one needs.Backups marked permanent (`backup-push --permanent` or `backup-mark`) are never deleted, and the WAL from their start to their stop is kept. The trap: PostgreSQL backups are ordered by the timeline and segment in their names, so after `pg_upgrade` or a fresh cluster in the same prefix the new backups sort as oldest; `--use-sentinel-time` orders by real start time, and a separate prefix per major version avoids the problem.

code

bash · 4 lines
bash
wal-g backup-list --pretty --detail
wal-g delete retain FULL 7                     # dry run: prints what would go
wal-g delete retain FULL 7 --confirm           # actually deletes
wal-g delete retain FULL 7 --use-sentinel-time --confirm   # after a pg_upgrade into the same prefix

go deeper

for a junior

Remember that wal-g delete is a dry run until you add --confirm, and that retain FULL N keeps N full backups with their deltas.

for a middle

Explain how retain, FULL and FIND_FULL count deltas, why WAL before the oldest kept backup is removed, and what permanent backups keep.

for a senior

Spot the pg_upgrade ordering trap, fix it with --use-sentinel-time or separate prefixes, and schedule deletes only after a successful backup-push.

for a principal

Turn a promised recovery window and the bucket's object lock into a retention setting, and decide which backups deserve permanent status.

## The delete command family `wal-g delete` removes backups and the WAL they no longer need. It is a **dry run by default**: it prints what it would delete and exits. Only with `--confirm` does anything disappear. | Mode | What it keeps or removes | |---|---| | `retain [FULL\|FIND_FULL] N [--after name\|time]` | keeps the newest N backups (or N full ones), deletes older backups and WAL | | `before [FIND_FULL] name` | deletes backups and WAL older than the named backup | | `target [FIND_FULL] name` | deletes one backup (and, for a full or delta, the deltas built on it); deletes **no WAL** | | `everything [FORCE]` | deletes all backups; `FORCE` includes permanent ones | | `garbage [ARCHIVES\|BACKUPS]` | PostgreSQL only: removes WAL and leftover objects older than the oldest non-permanent backup | ## What N counts The three forms of `retain` differ in what they count: - **`retain 7`** counts every backup, full or delta, and **fails** if the seventh is a delta, because keeping it without its base would leave a delta nobody can restore. - **`retain FULL 7`** counts only full backups and keeps every delta that depends on them, so with daily deltas the number of objects kept is much larger than seven. - **`retain FIND_FULL 7`** takes the seventh backup and walks back to the full backup it needs, keeping everything after that. - **`--after 2026-09-01T00:00:00`** (or a backup name) adds: keep N backups *and* everything made after that point. ## What happens to WAL Deleting a backup also deletes the archived WAL older than the **oldest backup that remains**. That is what bounds the recovery window: you can recover to any moment from the start of the oldest kept backup to the newest archived segment, and nothing before. Choosing N is choosing that window. A worked example: with a full `backup-push` every night and `retain FULL 7` run right after it, the oldest kept backup is six days old, so the window runs from that backup's start to now, about six to seven days. With weekly fulls and nightly deltas, the same `retain FULL 7` keeps roughly seven weeks. Count backups, then translate the count into time explicitly; the command itself never thinks in days. `delete target` is the exception: it deletes no WAL, which is why `delete garbage` exists to sweep WAL and partial objects left behind. ## Permanent backups A backup made with `wal-g backup-push --permanent`, or marked later with `wal-g backup-mark <name>` (and unmarked with `-i`), is skipped by every mode except `everything FORCE`. WAL-G also keeps the WAL segments from that backup's start to its finish, which is enough to restore it to a consistent state, not to roll forward from it. Marking a delta permanent marks the backups it depends on too. Use it for the quarterly snapshot the auditors want, not as a substitute for a longer retention window. ## The ordering trap after pg_upgrade For PostgreSQL, `delete retain` and `delete before` sort backups by the **timeline and WAL segment in the backup name** (`base_000000010000000000000004`). That order is chronological only while one cluster writes to the prefix. After `pg_upgrade`, or after you initialise a new cluster and point it at the same `WALG_S3_PREFIX`, the timeline and segment numbers start low again. The newest backups now look oldest, and `retain` can delete them while keeping the old cluster's backups you no longer need. Two defences: - `--use-sentinel-time` orders backups by the start time recorded in each backup's sentinel and metadata, falling back to name order for backups whose metadata cannot be read. - Better: give every major version, and every rebuilt cluster, **its own prefix**, so the problem cannot arise. ## A safe routine For a small startup's containerised PostgreSQL shipping to one bucket: 1. Run `wal-g backup-list --pretty --detail` and look at what is there. 2. Run the delete command **without** `--confirm` and read the dry-run list. 3. Run it again with `--confirm`, from the same scheduled job that runs `backup-push`, after the push has succeeded, never before it. 4. Alert if the oldest kept backup becomes younger than the recovery window you promised. If the bucket has object lock (`S3_RETENTION_PERIOD` with `S3_RETENTION_MODE=COMPLIANCE`), deletes of locked objects fail until the period passes, so set WAL-G's retention no shorter than the lock.

  • Why does wal-g delete retain 5 sometimes fail instead of deleting anything?
    Plain `retain 5` counts deltas as backups. If the fifth newest is a delta, keeping it while deleting its base would leave an unrestorable backup, so WAL-G refuses. Use `retain FULL 5` to count full backups and keep their deltas, or `retain FIND_FULL 5` to keep back to the full that the fifth backup needs.
  • After wal-g delete target removed a broken backup, storage use barely dropped. Why?
    `delete target` deletes the backup objects but deliberately no WAL, unlike `retain` and `before`. WAL older than the oldest remaining backup, and leftovers from failed pushes, stay until you run `wal-g delete garbage` (or `garbage ARCHIVES` / `garbage BACKUPS`) with `--confirm`.

  • wal-g delete retain deletes immediately, --confirm only silences a prompt
  • retain 7 keeps seven full backups even when deltas exist
  • Deleting old backups leaves the WAL archive untouched
  • WAL-G always orders backups by when they were taken
  • A permanent backup also keeps all WAL after it for later PITR