private final synchronized boolean applyDeletes() throws CorruptIndexException, IOException {
    assert testPoint("startApplyDeletes");
    flushDeletesCount++;
    SegmentInfos rollback = (SegmentInfos) segmentInfos.clone();
    boolean success = false;
    boolean changed;
    try {
      changed = docWriter.applyDeletes(segmentInfos);
      success = true;
    } finally {
      if (!success) {
        if (infoStream != null)
          message("hit exception flushing deletes");

        // Carefully remove any partially written .del
        // files
        final int size = rollback.size();
        for(int i=0;i<size;i++) {
          final String newDelFileName = segmentInfos.info(i).getDelFileName();
          final String delFileName = rollback.info(i).getDelFileName();
          if (newDelFileName != null && !newDelFileName.equals(delFileName))
            deleter.deleteFile(newDelFileName);
        }

        // Fully replace the segmentInfos since flushed
        // deletes could have changed any of the
        // SegmentInfo instances:
        segmentInfos.clear();
        segmentInfos.addAll(rollback);
      }
    }

    if (changed)
      checkpoint();
    return changed;
  }