Automate Deletion of Old Archive Logs Using RMAN
This shell script is part of the Shell Script Automations toolkit for Oracle DBAs. It helps automatically delete archive logs that are older than a day using RMAN. It also performs cleanup of expired and obsolete entries from the RMAN catalog. Ideal for environments where archive logs are not backed up and need to be purged regularly to free up space.
📜 Shell Script: rman_arch_del.sh
################################################################################
# Title : Automated Deletion of Old Archive Logs Using RMAN
# Script : rman_arch_del.sh
# Purpose : Deletes archive logs older than 1 day and cleans up RMAN metadata.
#
# Features:
# - Deletes archive logs older than SYSDATE - 1
# - Crosschecks archive logs and backups
# - Deletes expired and obsolete metadata
# - Cron-friendly and lightweight
#
# Author : W3Buddy
# Version : 1.2
################################################################################
#!/bin/bash
# Set Oracle Environment - Update as per your environment
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2.0
export ORACLE_SID=PARIS12C
export PATH=$ORACLE_HOME/bin:$PATH
# Run RMAN commands and log output
rman log=/home/oracle/arch_del.log <<EOF
CONNECT TARGET /
-- Delete archive logs older than 1 day
DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-1';
-- Validate catalog entries
CROSSCHECK ARCHIVELOG ALL;
CROSSCHECK BACKUP;
-- Remove expired entries
DELETE EXPIRED ARCHIVELOG ALL;
DELETE EXPIRED BACKUP;
-- Remove obsolete backups using policy
DELETE NOPROMPT OBSOLETE;
EXIT
EOF
✅ NOTE: This script avoids the use of
DELETE FORCE OBSOLETE
to prevent accidental deletion of useful backups. Add it only if you’re certain it’s required.
⚙️ Setup Instructions
# ----------------------------------------------
# 1. Create the script file
# ----------------------------------------------
cd /u01/app/oracle
vi rman_arch_del.sh
# (Paste the full script above into the file and save)
# ----------------------------------------------
# 2. Make the script executable
# ----------------------------------------------
chmod +x rman_arch_del.sh
# ----------------------------------------------
# 3. Test the script manually
# ----------------------------------------------
./rman_arch_del.sh
# Check the log file for details:
cat /home/oracle/arch_del.log
# ----------------------------------------------
# 4. Schedule with cron (every night at 10:00 PM)
# ----------------------------------------------
crontab -e
# Add this line:
00 22 * * * /u01/app/oracle/rman_arch_del.sh >> /tmp/rmanarch.log 2>&1
# ----------------------------------------------
# 5. Confirm the cron entry
# ----------------------------------------------
crontab -l