Shell script to delete Oracle Database Archive logs


I am going to share a shell script to delete archive logs older than 2 days in Oracle Database. We can change the number of days as per requirement.

Create Shell script

vi /home/oracle/scripts/del_arch_rman.sh

Add the Below code in the script

#!/bin/bash
export ORACLE_HOME=/u01/app/oracle/product/12.1.0
export ORACLE_SID=FUNDB
export PATH=$ORACLE_HOME/bin:$PATH

###Function to delete archive logs###

delarch () {
rman log=tmp/delete_arch.log << EOF
connect target /
DELETE noprompt ARCHIVELOG ALL COMPLETED BEFORE 'sysdate-2';
CROSSCHECK ARCHIVELOG ALL;
DELETE EXPIRED ARCHIVELOG ALL;
exit
EOF
}

#############################
#Main to call archive delete#
echo "Deleting Archive Logs"
delarch
echo "Successfully deleted archives"

Save the script.

You can run manually or via crontab as per requirement

Schedule in crontab:

00 4 * * * /home/oracle/scripts/del_arch_rman.sh > /tmp/del_archive.log 2>&1







If you like please follow and comment