@Transactional
  static void fixAlertsForDeletedServices() {

    Configuration conf = injector.getInstance(Configuration.class);

    LOG.info("fixAlertsForDeletedServices stale alert definitions for deleted services");

    ensureConnection();

    String SELECT_STALE_ALERT_DEFINITIONS = "select definition_id from alert_definition where service_name not in " +
            "(select service_name from clusterservices) and service_name not in ('AMBARI')";

    int recordsCount = 0;
    Statement statement = null;
    ResultSet rs = null;
    List<Integer> alertIds = new ArrayList<Integer>();
    try {
      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = statement.executeQuery(SELECT_STALE_ALERT_DEFINITIONS);
      while (rs.next()) {
        alertIds.add(rs.getInt("definition_id"));
      }

    } catch (SQLException e) {
      warning("Exception occurred during fixing stale alert definitions: ", e);
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
          LOG.error("Exception occurred during fixing stale alert definitions: ", e);
        }
      }
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {
          LOG.error("Exception occurred during fixing stale alert definitions: ", e);
        }
      }
    }

    for(Integer alertId : alertIds) {
      final AlertDefinitionEntity entity = alertDefinitionDAO.findById(alertId.intValue());
      alertDefinitionDAO.remove(entity);
    }
    warning("fixAlertsForDeletedServices - {}  Stale alerts were deleted", alertIds.size());

  }
This method checks if there are any ConfigGroup host mappings with hosts
that are not longer a part of the cluster.
ambari-server/src/main/java/org/apache/ambari/server/checks/DatabaseConsistencyCheckHelper.java