Oracle RMAN Delete Obsolete - Remove Unneeded Backups

Affiliate Disclosure: This post contains affiliate links. Oracle Scripts may earn a commission if you buy through them, at no extra cost to you.

Oracle RMAN Delete Obsolete - Remove Unneeded Backups

Purpose

A backup disk fills up slowly. Every night adds another set of backup files, and nobody removes the old ones. One morning the nightly backup fails because there is no space left. RMAN already knows which backups are no longer needed, but it does not remove them on its own. The DELETE OBSOLETE command does that job.

RMAN decides what is obsolete by applying the retention policy. The policy is either a redundancy count (how many full backups to keep) or a recovery window (how many days back you must be able to recover). A backup that the policy no longer needs is obsolete. It uses space and gives you no extra recovery options.

This post shows the command and its variations. It explains how the two policy types differ, which items the command can remove, and which checks to run first. It also covers the cases where the command does not behave the way many DBAs expect.

Code

1DELETE OBSOLETE;

Variations of the same command:

 1-- Basic command (prompts for confirmation)
 2DELETE OBSOLETE;
 3
 4-- Delete without confirmation prompt
 5DELETE NOPROMPT OBSOLETE;
 6
 7-- Delete with custom recovery window
 8DELETE OBSOLETE RECOVERY WINDOW OF 7 DAYS;
 9
10-- Delete with custom redundancy
11DELETE OBSOLETE REDUNDANCY 2;
12
13-- Delete obsolete backups for specific device type
14DELETE OBSOLETE DEVICE TYPE DISK;

A check on the repository after a delete, run in SQL*Plus on the target database:

1SELECT status, COUNT(*) AS pieces
2FROM   v$backup_piece
3GROUP  BY status;

Code Breakdown

DELETE OBSOLETE

Oracle describes the command as deleting data file backups and copies recorded in the RMAN repository that are no longer needed. RMAN also deletes obsolete archived redo log files and log backups. For each file it removes, RMAN does three things:

  1. It deletes the physical backup or copy.
  2. It updates the repository record in the target control file to show that the file is deleted. In V$BACKUP_PIECE, the STATUS value for such a piece is D.
  3. It removes the repository record from the recovery catalog, if you use a catalog.

If you use no recovery catalog, the third step does not apply. The control file is the only repository.

How RMAN picks what is obsolete

RMAN first uses the options you give in the command, such as RECOVERY WINDOW or REDUNDANCY. If you give none, it uses the setting from CONFIGURE RETENTION POLICY. This is why the plain command and the variations can delete different sets of files on the same system.

NOPROMPT

When you run RMAN interactively, DELETE lists the files and asks for confirmation before it deletes any of them. When RMAN reads its commands from a command file, it does not prompt. NOPROMPT skips both the list and the question. RMAN still displays each item as it is deleted.

RECOVERY WINDOW and REDUNDANCY

Both options override the configured policy for one run. RECOVERY WINDOW OF n DAYS removes backups and copies that are not needed to recover the database to any point within the last n days. REDUNDANCY n treats a backup as obsolete when at least n more recent backup sets or image copies of the same data file exist. For example, REDUNDANCY 2 means that two newer backups of a data file must exist before an older one is obsolete. Oracle states that you cannot use both options in a single REPORT OBSOLETE or DELETE OBSOLETE command.

DEVICE TYPE

DEVICE TYPE DISK limits the deletion to obsolete backups and copies created on that device type. Backups on other devices, such as tape, are left alone.

The query

The SELECT against V$BACKUP_PIECE counts backup pieces by status. After a successful delete, you should see pieces with status D instead of the pieces you removed. This is a quick check that the control file reflects the deletion.

Key Points

  • The retention policy is the default rule. The command uses it unless you give RECOVERY WINDOW or REDUNDANCY in the command itself.
  • The default policy is REDUNDANCY 1. CONFIGURE RETENTION POLICY CLEAR returns the setting to that default.
  • A retention policy applies only to full or level 0 data file and control file backups. Level 1 incremental backups and archived redo logs become obsolete when no full or level 0 backup is left for them to apply to.
  • A backup made with the KEEP UNTIL TIME clause becomes obsolete when the keep time passes, whatever the configured retention policy says.
  • The archived log deletion policy does not control this command. Oracle states that DELETE OBSOLETE considers only the backup retention policy. The DELETE ARCHIVELOG ALL command is the one that considers only the archived log deletion policy.
  • DELETE OBSOLETE cannot be used when backups are stored on a Zero Data Loss Recovery Appliance.
  • With a fast recovery area, the database also deletes obsolete files by itself when it needs space, but it never deletes files in violation of the retention policy to do so.

Understanding Retention Policies

Redundancy-based policy

A redundancy-based policy says how many full or level 0 backups of each data file and control file to keep:

1CONFIGURE RETENTION POLICY TO REDUNDANCY 2;

With this setting RMAN keeps two such backups. If more exist, RMAN marks the extra files as obsolete. This setting considers only the current set of data files.

Recovery window-based policy

A recovery window policy says how far back you must be able to recover:

1CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;

The window runs from the current time back to the point of recoverability, which is the current time minus the number of days. Backups that are needed to recover to any point inside that window are kept. Older backups that are not needed for that purpose are obsolete.

One policy at a time

The two options are mutually exclusive. Only one type of retention policy can be in effect at any time. Setting one replaces the other.

The NONE setting

CONFIGURE RETENTION POLICY TO NONE disables the feature. RMAN then does not consider any backup set or copy as obsolete, so a plain DELETE OBSOLETE has nothing to remove. This is a common reason for the "expected to delete, nothing happened" case described below.

Insights and Best Practices

Check the current retention policy

Before you delete anything, look at the setting:

1RMAN> SHOW ALL;

SHOW ALL displays all user-entered CONFIGURE commands and the default configurations. Look for the CONFIGURE RETENTION POLICY line. A default value is marked with # default at the end of the line. To see only the policy, use SHOW RETENTION POLICY.

Report before you delete

Preview the files first:

1RMAN> REPORT OBSOLETE;

REPORT OBSOLETE lists the full backups, data file copies and archived redo log files in the repository that can be deleted because they are no longer needed. It uses the same rules as the delete command, so the list shows what the plain DELETE OBSOLETE would target. When you plan to use an override, give it to the report as well:

1RMAN> REPORT OBSOLETE RECOVERY WINDOW OF 7 DAYS;

Read the list before you confirm the delete. The report is the cheap place to find a wrong policy.

Run a crosscheck first

Oracle's reference for the DELETE command gives this as the best practice: run CROSSCHECK to update the status of backups and copies in the repository, then run DELETE to remove the files you want.

1RMAN> CROSSCHECK BACKUP;
2RMAN> DELETE OBSOLETE;

The reason is that the repository can be wrong about the files. If someone removes a disk backup with the operating system rm command, the repository record does not show that the file is gone. A crosscheck marks files that RMAN cannot find as EXPIRED. Oracle's cloud backup guidance also recommends a crosscheck before you run DELETE OBSOLETE.

Obsolete is not the same as expired

These two words describe different problems:

  • Obsolete: the file exists, but the retention policy no longer needs it. Use DELETE OBSOLETE.
  • Expired: the repository lists the file, but the crosscheck could not find it. Use DELETE EXPIRED.

DELETE EXPIRED removes only repository records whose status is EXPIRED. If a backup marked EXPIRED turns out to exist when the command runs, RMAN does not delete the physical file. In short, obsolete means not needed, and expired means not found.

Put the cleanup after the backup

A scheduled backup script can end with the delete. A crosscheck and a NOPROMPT delete in a RUN block look like this:

1RMAN> RUN {
2  CROSSCHECK BACKUP;
3  DELETE NOPROMPT OBSOLETE;
4}

Because the command file case never prompts, NOPROMPT is not strictly needed in a script, but writing it makes the intent clear to the next reader. The NOPROMPT form also prints each item as it is deleted, so the job log records what was removed.

Use a recovery window with care

A window of seven days means that every backup needed to reach any point in the last seven days is kept. This can include a full backup that is older than seven days, because the older full backup is the base for the window. If your disk plan counts only seven days of files, check the report first.

Allocate the right channel

RMAN uses all configured channels to perform the deletion. If the files are on a device that has no automatic channel, you must allocate a maintenance channel yourself. A backup made through an SBT (tape) channel cannot be deleted when only a disk channel is configured. In that case allocate an SBT maintenance channel for the delete. A disk-only file needs an automatic or manual maintenance channel as well.

Data Guard note

In a Data Guard setup, RMAN treats tape backups made on any database as available to all databases in the environment. Disk backups are available only to the database that created them. Plan the delete on each database with that in mind.

When to Run This

  • At the end of each scheduled backup job, after a crosscheck, to keep the backup area from growing without limit.
  • When a disk or fast recovery area alert shows that backup files are using too much space.
  • After you change the retention policy. A shorter policy makes older backups obsolete at once, so run REPORT OBSOLETE first.
  • After a manual file cleanup at the operating system level, followed by CROSSCHECK and DELETE EXPIRED, to bring the repository back in line with the disk.
  • Before a maintenance window, to check that the backups you need for the recovery window are still there.

Troubleshooting Common Issues

The command waits for input. In an interactive RMAN session, DELETE lists the files and asks for confirmation. Add NOPROMPT to skip the list and the question:

1DELETE NOPROMPT OBSOLETE;

RMAN reports no obsolete backups, but you expect some. Check the retention policy with SHOW RETENTION POLICY. If it is NONE, RMAN does not consider any backup obsolete. With the policy set to NONE, REPORT OBSOLETE with no other options also returns an error. To return to the default, run:

1RMAN> CONFIGURE RETENTION POLICY CLEAR;

This sets the policy back to REDUNDANCY 1. A very long recovery window can also leave nothing to delete.

Archived logs you wanted to keep were deleted. An archived log deletion policy does not protect logs from this command. DELETE OBSOLETE uses only the backup retention policy. If you need to keep logs longer, set the backup retention so they stay needed, or review the report before you delete.

A message about a missing channel. The deletion needs a channel that matches the device where the file lives. Allocate a maintenance channel for the right device type, as shown above.

The command is refused on a Recovery Appliance. Oracle states that DELETE OBSOLETE cannot be used when backups are stored to a Zero Data Loss Recovery Appliance. Use the appliance tools for those backups.

Files still listed after you removed them by hand. The repository does not know about a file removed with rm. Run CROSSCHECK BACKUP;, then DELETE EXPIRED BACKUP;, to clear the stale records.

Related books recommended

Links go to Amazon. As an Amazon Associate, Oracle Scripts earns from qualifying purchases; your price is unchanged.

References

Posts in this series