.. versionadded:: 2017.7.3

    Ensures that a file is not present in the minion's cache, deleting it
    if found. This state is primarily invoked by other states to ensure
    that a fresh copy is fetched.

    name
        The URL of the file to be removed from cache. To remove a file from
        cache in an environment other than ``base``, either use the ``saltenv``
        argument or include the saltenv in the URL (e.g.
        ``salt://path/to/file.conf?saltenv=dev``).

        .. note::
            A list of URLs is not supported, this must be a single URL. If a
            local file is passed here, the state will take no action.

    saltenv
        Used to specify the environment from which to download a file from the
        Salt fileserver (i.e. those with ``salt://`` URL).

def not_cached(name, saltenv='base'):
    '''
    .. versionadded:: 2017.7.3

    Ensures that a file is not present in the minion's cache, deleting it
    if found. This state is primarily invoked by other states to ensure
    that a fresh copy is fetched.

    name
        The URL of the file to be removed from cache. To remove a file from
        cache in an environment other than ``base``, either use the ``saltenv``
        argument or include the saltenv in the URL (e.g.
        ``salt://path/to/file.conf?saltenv=dev``).

        .. note::
            A list of URLs is not supported, this must be a single URL. If a
            local file is passed here, the state will take no action.

    saltenv
        Used to specify the environment from which to download a file from the
        Salt fileserver (i.e. those with ``salt://`` URL).
    '''
    ret = {'changes': {},
           'comment': '',
           'name': name,
           'result': False}

    try:
        parsed = _urlparse(name)
    except Exception:
        ret['comment'] = 'Only URLs or local file paths are valid input'
        return ret
    else:
        if parsed.scheme in salt.utils.files.LOCAL_PROTOS:
            full_path = os.path.realpath(os.path.expanduser(parsed.path))
            ret['result'] = True
            ret['comment'] = (
                'File {0} is a local path, no action taken'.format(
                    full_path
                )
            )
            return ret

    local_copy = __salt__['cp.is_cached'](name, saltenv=saltenv)

    if local_copy:
        try:
            os.remove(local_copy)
        except Exception as exc:
            ret['comment'] = 'Failed to delete {0}: {1}'.format(
                local_copy, exc.__str__()
            )
        else:
            ret['result'] = True
            ret['changes']['deleted'] = True
            ret['comment'] = '{0} was deleted'.format(local_copy)
    else:
        ret['result'] = True
        ret['comment'] = '{0} is not cached'.format(name)
    return ret