final HttpServletResponse resp) throws IOException, JSONException {
final Couch couch = getCouch(req);
final Set<String> dbKeep = new HashSet<String>();
final JSONArray databases = couch.getAllDatabases();
for (int i = 0; i < databases.length(); i++) {
final Database db = couch.getDatabase(databases.getString(i));
final UUID uuid = db.getUuid();
if (uuid == null) {
continue;
}
dbKeep.add(uuid.toString());
final Set<String> viewKeep = new HashSet<String>();
for (final DesignDocument ddoc : db.getAllDesignDocuments()) {
for (final View view : ddoc.getAllViews().values()) {
viewKeep.add(view.getDigest());
}
}
// Delete all indexes except the keepers.
final File[] dirs = DatabaseIndexer.uuidDir(root, db.getUuid()).listFiles();
if (dirs != null) {
for (final File dir : dirs) {
if (!viewKeep.contains(dir.getName())) {
LOG.info("Cleaning old index at " + dir);
FileUtils.deleteDirectory(dir);