#!/usr/bin/python #clean_patch_storage.py: Manually clean $ORACLE_HOME/.patch_storage by removing directories not shown in `opatch lsinventory'. #See "Remove all the sub-directories from $ORACLE_HOME/.patch_storage that are not present in the list of installed patches" in #How To Avoid Disk Full Issues Because OPatch Backups Take Big Amount Of Disk Space. (Doc ID 550522.1) import sys, os, string, re #Must pass ORACLE_HOME, either GI or DB HOME, e.g. /u01/app/19.0.0/grid or /u01/app/oracle/product/19.0.0/db if len(sys.argv) == 1: sys.exit('Please pass ORACLE_HOME directory as the argument, e.g. ' + sys.argv[0] + ' /u01/app/19.0.0/grid') OH = sys.argv[1] ps = os.listdir(OH + '/.patch_storage') lsinv = os.popen('ORACLE_HOME=' + OH + ' ' + OH + '/OPatch/opatch lsinventory | grep ": applied on"').readlines() ps2 = [] #new list of patch storage files for i in ps: match = re.search('^[0-9]+', i) if match: ps2.append(match.group(0)) lsinv2 = [] #new list of lsinventory result for i in lsinv: match = re.search('^Patch ([0-9]+)', i) if match: lsinv2.append(match.group(1)) if len(set(ps2) - set(lsinv2)) == 0: sys.exit('Nothing should be deleted from ' + OH + '/.patch_storage!') else: print('You can safely run the commands below:') for i in set(ps2)-set(lsinv2): print('rm -rf ' + OH + '/.patch_storage/' + i + '*') #os.popen('rm -rf ' + OH + '/.patch_storage/' + i + '*') #if you want the script to actually rm them