human
Complete the below
def destroy_cached_images(session, sr_ref, all_cached=False, dry_run=False):

    ''''Destroy used or unused cached images.
    A cached image that is being used by at least one VM is said to be \'used\'.
    In the case of an \'unused\' image, the cached image will be the only
    descendent of the base-copy. So when we delete the cached-image, the
    refcount will drop to zero and XenServer will automatically destroy the
    base-copy for us.
    The default behavior of this function is to destroy only \'unused\' cached
    images. To destroy all cached images, use the `all_cached=True` kwarg.'
    '''
assistant
#Here is the Python code you requested.
    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