@SuppressWarnings("unchecked")
  private void removeDeletedResources(
      Map<Integer, List<AppdefEntityID>> agentCache, Collection<String> typeNames)
      throws ApplicationException, VetoException {
    final boolean debug = log.isDebugEnabled();
    final StopWatch watch = new StopWatch();
    final AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);
    if (debug) watch.markTimeBegin("unscheduleMeasurementsForAsyncDelete");
    unscheduleMeasurementsForAsyncDelete(agentCache);
    if (debug) watch.markTimeEnd("unscheduleMeasurementsForAsyncDelete");

    // Look through services, servers, platforms, applications, and groups
    if (debug) watch.markTimeBegin("removeApplications");
    Collection<Application> applications = applicationManager.findDeletedApplications();
    removeApplications(subject, applications);
    if (debug) watch.markTimeEnd("removeApplications");

    if (debug) watch.markTimeBegin("removeResourceGroups");
    Collection<ResourceGroup> groups = resourceGroupManager.findDeletedGroups();
    removeResourceGroups(subject, groups);
    if (debug) watch.markTimeEnd("removeResourceGroups");

    typeNames = (typeNames == null) ? Collections.EMPTY_LIST : typeNames;
    if (debug) watch.markTimeBegin("removeGroupsCompatibleWith");
    for (String name : typeNames) {
      resourceGroupManager.removeGroupsCompatibleWith(name);
    }
    if (debug) watch.markTimeEnd("removeGroupsCompatibleWith");

    Collection<Service> services = serviceManager.findDeletedServices();
    removeServices(subject, services);

    Collection<Server> servers = serverManager.findDeletedServers();
    removeServers(subject, servers);

    if (debug) watch.markTimeBegin("removePlatforms");
    Collection<Platform> platforms = platformManager.findDeletedPlatforms();
    removePlatforms(subject, platforms);
    if (debug) watch.markTimeEnd("removePlatforms");
    if (debug) log.debug("removeDeletedResources: " + watch);
  }
  /**
   * Disable measurements and unschedule from the agent in bulk with the agent cache info because
   * the resources have been de-referenced from the agent
   *
   * @param agentCache {@link Map} of {@link Integer} of agentIds to {@link List} of {@link
   *     AppdefEntityID}s
   */
  private void unscheduleMeasurementsForAsyncDelete(Map<Integer, List<AppdefEntityID>> agentCache) {
    if (agentCache == null) {
      return;
    }

    try {
      AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);

      for (Integer agentId : agentCache.keySet()) {

        Agent agent = agentManager.getAgent(agentId);
        List<AppdefEntityID> resources = agentCache.get(agentId);

        measurementManager.disableMeasurementsForDeletion(
            subject,
            agent,
            (AppdefEntityID[]) resources.toArray(new AppdefEntityID[resources.size()]));
      }
    } catch (Exception e) {
      log.error("Error unscheduling measurements during async delete", e);
    }
  }