def destroy_cached_images(session, sr_ref, all_cached=False, dry_run=False):
 cached_images = _find_cached_images(session, sr_ref)
 destroyed = set()
 def destroy_cached_vdi(vdi_uuid, vdi_ref):
  LOG.debug(_("Destroying cached VDI '%(vdi_uuid)s'"))
  if (not dry_run):
   destroy_vdi(session, vdi_ref)
  destroyed.add(vdi_uuid)
 for vdi_ref in cached_images.values():
  vdi_uuid = session.call_xenapi('VDI.get_uuid', vdi_ref)
  if all_cached:
   destroy_cached_vdi(vdi_uuid, vdi_ref)
   continue
  chain = list(_walk_vdi_chain(session, vdi_uuid))
  if (len(chain) > 2):
   continue
  elif (len(chain) == 2):
   root_vdi_rec = chain[(-1)]
   children = _child_vhds(session, sr_ref, root_vdi_rec['uuid'])
   if (len(children) > 1):
    continue
  destroy_cached_vdi(vdi_uuid, vdi_ref)
 return destroyed

def destroy_cached_images ( session , sr_ref , all_cached = False , dry_run = False ) : cached_images = _find_cached_images ( session , sr_ref ) destroyed = set ( ) def destroy_cached_vdi ( vdi_uuid , vdi_ref ) : LOG . debug ( _ ( "Destroying cached VDI '%(vdi_uuid)s'" ) ) if ( not dry_run ) : destroy_vdi ( session , vdi_ref ) destroyed . add ( vdi_uuid ) for vdi_ref in cached_images . values ( ) : vdi_uuid = session . call_xenapi ( 'VDI.get_uuid' , vdi_ref ) if all_cached : destroy_cached_vdi ( vdi_uuid , vdi_ref ) continue chain = list ( _walk_vdi_chain ( session , vdi_uuid ) ) if ( len ( chain ) > 2 ) : continue elif ( len ( chain ) == 2 ) : root_vdi_rec = chain [ ( - 1 ) ] children = _child_vhds ( session , sr_ref , root_vdi_rec [ 'uuid' ] ) if ( len ( children ) > 1 ) : continue destroy_cached_vdi ( vdi_uuid , vdi_ref ) return destroyed
destroy used or unused cached images .