Oracle RMAN Delete Obsolete Backups with Maintenance Channel

Oracle RMAN Delete Obsolete Backups with Maintenance Channel

Purpose

The Oracle RMAN (Recovery Manager) commands shown in this script help database administrators manage backup storage by automatically removing old, unnecessary backup files. These commands allocate a maintenance channel for disk operations and delete obsolete backups based on specific redundancy policies, freeing up valuable storage space while maintaining the required number of backup copies for database recovery.

Code

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
2DELETE OBSOLETE REDUNDANCY = 4 device type disk; 
3DELETE OBSOLETE REDUNDANCY = 2 device type disk;

Code Breakdown

Line 1: ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;

This command manually allocates a maintenance channel for disk-based operations. A maintenance channel is required before executing CHANGE, DELETE, or CROSSCHECK commands in RMAN. The maintenance channel tells RMAN which storage device (in this case, DISK) to use when managing backup files.

Key characteristics:

  • Must be executed at the RMAN prompt, not inside a RUN block
  • Creates a channel named using the convention ORA_MAINT_DISK_n where n is the channel number
  • Provides direct access to the storage location where backups reside
  • Prevents the error "RMAN-06091: no channel allocated for maintenance"

Line 2: DELETE OBSOLETE REDUNDANCY = 4 DEVICE TYPE DISK

1DELETE OBSOLETE REDUNDANCY = 4 device type disk;

This command deletes obsolete backups while keeping four copies of each backup file. RMAN considers backups obsolete when they exceed the specified redundancy count and are no longer required for database recovery.

What happens when this executes:

  • Physical backup files are removed from the disk filesystem
  • Backup entries are deleted from the RMAN recovery catalog
  • Entries are marked as DELETED in the Oracle control file

The REDUNDANCY = 4 parameter means RMAN will keep the four most recent backup copies and delete any older backups beyond this limit.

Line 3: delete obsolete REDUNDANCY = 2 device type disk

1delete obsolete REDUNDANCY = 2 device type disk;

This command performs the same operation as Line 2 but with a different redundancy policy. It keeps only two backup copies and marks all older backups as obsolete for deletion. This more aggressive retention policy frees up more storage space but provides fewer recovery options.

Important note: If this command runs after Line 2, it will delete the backups that were kept by the previous command, reducing the total number of retained backups from four to two.

Key Points

  1. Maintenance channels are essential - You must allocate a maintenance channel before running DELETE or CROSSCHECK commands to avoid errors
  2. Redundancy controls retention - The REDUNDANCY parameter specifies how many backup copies to keep; backups exceeding this number become obsolete
  3. Three-step deletion process - DELETE OBSOLETE removes physical files, updates the recovery catalog, and marks entries as deleted in the control file
  4. Device type specificity - The DEVICE TYPE DISK clause restricts operations to disk-based backups only, leaving tape backups untouched
  5. Conflicting redundancy values - Running two DELETE OBSOLETE commands with different redundancy values in sequence may cause the second command to delete backups retained by the first
  6. Manual vs automatic channels - While manual allocation works, Oracle recommends configuring automatic channels that persist across RMAN sessions

Insights and Best Practices

Run CROSSCHECK Before Deletion

Always execute a CROSSCHECK command before DELETE OBSOLETE to synchronize the RMAN repository with actual physical backup files:

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
2CROSSCHECK BACKUP;
3DELETE OBSOLETE REDUNDANCY = 2 device type disk;
4RELEASE CHANNEL;

This ensures RMAN has accurate information about which files exist on disk.

Use NOPROMPT for Automation

For automated scripts, add the NOPROMPT option to skip confirmation prompts:

1DELETE NOPROMPT OBSOLETE REDUNDANCY = 2 device type disk;

This is ideal for scheduled maintenance scripts that run without human intervention.

Consider Recovery Requirements

Choose your redundancy value based on your recovery needs. A higher redundancy number (like 4) provides more recovery options but uses more storage space, while a lower number (like 2) saves space but offers fewer recovery points.

Release Channels After Use

Although maintenance channels are released automatically at session end, explicitly releasing them helps avoid confusion during complex sessions:

1RELEASE CHANNEL;

Configure Automatic Channels

Instead of manually allocating maintenance channels, configure automatic channels for simpler management:

1CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/backup/%U';
2DELETE OBSOLETE REDUNDANCY = 2;

This approach reduces human error and eliminates the need to remember exact syntax for each operation.

Inconsistent Retention Policies

The script shows two different redundancy values (4 and 2) being applied sequentially. This is not a recommended practice because the second command will delete backups that the first command intended to keep. Instead, choose one consistent retention policy:

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
2DELETE OBSOLETE REDUNDANCY = 2 device type disk;
3RELEASE CHANNEL;

Regular Cleanup Schedule

Run DELETE OBSOLETE regularly (daily or weekly) to prevent storage space issues. You can automate this using operating system scheduling utilities or include it in your backup scripts.

Common Use Cases

Daily automated cleanup after backups:

1run {
2  backup database plus archivelog;
3  delete noprompt obsolete redundancy = 2;
4}

Cleanup for specific device types:

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
2DELETE OBSOLETE REDUNDANCY = 3 DEVICE TYPE DISK;
3RELEASE CHANNEL;

Managing multiple device types:

1ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
2ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE SBT_TAPE;
3DELETE OBSOLETE;
4RELEASE CHANNEL;

References

Posts in this series