# Buggy code
def clear_old_remotes(self):
    """
    Remove cache directories for remotes no longer configured
    """
    try:
        cachedir_ls = os.listdir(self.cache_root)
    except OSError:
        cachedir_ls = []
    # Remove actively-used remotes from list
    for repo in self.remotes:
        try:
            cachedir_ls.remove(repo.cachedir_basename)
        except ValueError:
            pass
    to_remove = []
    for item in cachedir_ls:
        if item in ("gitfs", "refs"):
            continue
        path = os.path.join(self.cache_root, item)
        if os.path.isdir(path):
            to_remove.append(path)
    failed = []
    if to_remove:
        for rdir in to_remove:
            try:
                shutil.rmtree(rdir)
            except OSError as exc:
                log.error(
                    "Unable to remove old {0} remote cachedir {1}: {2}".format(
                        self.role, rdir, exc
                    )
                )
                failed.append(rdir)
            else:
                log.debug("{0} removed old cachedir {1}".format(self.role, rdir))
    for fdir in failed:
        to_remove.remove(fdir)
    ret = bool(to_remove)
    if ret:
        self.write_remote_map()
    return ret


# Traceback (IOError)
Sep 17 18:55:12 [DEBUG   ] LazyLoaded local_cache.clean_old_jobs
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Updating fileserver for git_pillar module
Sep 17 18:55:12 [DEBUG   ] Could not LazyLoad config.merge
Sep 17 18:55:12 [DEBUG   ] Updating git fileserver cache
Sep 17 18:55:12 [DEBUG   ] gitfs removed old cachedir /var/cache/salt/master/gitfs/gitfs
Sep 17 18:55:12 [INFO    ] Wrote new gitfs remote map to /var/cache/salt/master/gitfs/remote_map.txt
Sep 17 18:55:12 [DEBUG   ] Set lock for file:///repo.git
Sep 17 18:55:12 [DEBUG   ] gitfs is fetching from 'file:///repo.git'
Sep 17 18:55:12 [ERROR   ] Error in function _file_hash:
Sep 17 18:55:12 Traceback (most recent call last):
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
Sep 17 18:55:12     ret = getattr(self, func)(load)
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 528, in file_hash
Sep 17 18:55:12     fnd = self.find_file(load['path'], load['saltenv'])
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/fileserver/__init__.py", line 482, in find_file
Sep 17 18:55:12     fnd = self.servers[fstr](path, saltenv, **kwargs)
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/fileserver/gitfs.py", line 143, in find_file
Sep 17 18:55:12     return gitfs.find_file(path, tgt_env=tgt_env, **kwargs)
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/utils/gitfs.py", line 2100, in find_file
Sep 17 18:55:12     with salt.utils.fopen(lk_fn, 'w+') as fp_:
Sep 17 18:55:12   File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1208, in fopen
Sep 17 18:55:12     fhandle = open(*args, **kwargs)
Sep 17 18:55:12 IOError: [Errno 2] No such file or directory: '/var/cache/salt/master/gitfs/gitfs/hash/path/to/my/file.jinja.lk'
Sep 17 18:55:12 [DEBUG   ] Removed lock for file:///repo.git

# Fixed code
def clear_old_remotes(self):
    """
    Remove cache directories for remotes no longer configured
    """
    try:
        cachedir_ls = os.listdir(self.cache_root)
    except OSError:
        cachedir_ls = []
    # Remove actively-used remotes from list
    for repo in self.remotes:
        try:
            cachedir_ls.remove(repo.cachedir_basename)
        except ValueError:
            pass
    to_remove = []
    for item in cachedir_ls:
        if item in ("hash", "refs"):
            continue
        path = os.path.join(self.cache_root, item)
        if os.path.isdir(path):
            to_remove.append(path)
    failed = []
    if to_remove:
        for rdir in to_remove:
            try:
                shutil.rmtree(rdir)
            except OSError as exc:
                log.error(
                    "Unable to remove old {0} remote cachedir {1}: {2}".format(
                        self.role, rdir, exc
                    )
                )
                failed.append(rdir)
            else:
                log.debug("{0} removed old cachedir {1}".format(self.role, rdir))
    for fdir in failed:
        to_remove.remove(fdir)
    ret = bool(to_remove)
    if ret:
        self.write_remote_map()
    return ret