DB2 9.5 deletes previous backup images, log files and copies of load images automatically. This alleviates lots of pain for maintaining DB2 backups and log files.

Prior to DB2 9.5, these tasks were usually attained by the operating systems scripts. For example,

Taking an online backup and retaining only 5 most recent backups

$ db2 "BACKUP DATABASE dbname ONLINE TO /bkp/db2 COMPRESS UTIL_IMPACT_PRIORITY 50 INCLUDE LOGS WITHOUT PROMPTING"
$ find /bkp/db2 -mtime +5 | xargs rm

You should know what you are doing with the above command before you use it as it will delete backups older than 5 days. The above is the brute force method and I personally do not like such methods even though I have used them.

A nicer way will be to allow DB2 9.5 to do the job for you.

You can do this in 2 ways.

1. Let DB2 delete old backups, logs and copy of LOAD files automatically.
2. Let DB2 delete old backups, logs and copy of LOAD files when you want it to be done.

Let DB2 delete old backups automatically

To allow DB2 to delete old backups, old log files and old copies of LOAD images, you will need to turn parameter AUTO_DEL_REC_OBJ to ON.

db2 connect to sample
db2 update db cfg using AUTO_DEL_REC_OBJ on
db2 terminate

For automatic deletion, you will have to decide what value parameter REC_HIS_RETENTN should hold. The parameter REC_HIS_RETENTN tells to DB2 to prune the history file after ‘n’ number of days. The default value set is 366 days. If I take online backups every day, I may set this value to be 2 days. If I take off-line backup every Sunday followed by the delta backups every day, I may set this value to 8 days to make sure that my history file has information for the last 8 days (7 days + 1). Please remember that history file is very important for DB2 recovery operations. If by any chance you prune history, you can always restore history file from the the backup image.

Let us assume the value of REC_HIS_RETENTN to be 8.

db2 update db cfg for sample using REC_HIS_RETENTN 8

DB2 will start pruning history file automatically after 8 days and since parameter AUTO_DEL_REC_OBJ is set to ON, the recovery objects (backup, logs and load images) will be deleted automatically.

Let DB2 delete old backups automatically when you say it so

If you want to control when DB2 should delete the old recovery objects, you still need to set parameter AUTO_DEL_REC_OBJ to ON and DB2 will delete the old recovery objects when you use PRUNE command to delete entries from the history file.

db2 connect to sample
db2 update db cfg using AUTO_DEL_REC_OBJ ON
db2 terminate

DB2 will delete the old recovery objects when you issue PRUNE HISTORY command. For example,

To remove the entries for all restores, loads, table space backups, and full database backups taken before and including December 31, 2007 from the recovery history file, enter:

$ db2 prune history 19941231

Since you are deleting entries from history file, DB2 will also delete backups, logs and load copy images prior to Dec 31st, 2007.

This way, you are controlling the deletion of old recovery objects when you issue PRUNE HISTORY command.

The above is a much better method than old brute force method.