11

How can I flush/delete/erase all the index files/data in the disk using Apache Lucene.This is my code so far and still I can't remove index files.Please help me out...

Test:

public class Test {
    private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing"; 
    public static void main(String[] args) {

         try {
            ContentIndexer contentIndexer = new ContentIndexer(INDEX_DIR);
            contentIndexer.flushDisk();
            System.out.println("Flushed");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ContentIndexer:

public class ContentIndexer {
    private IndexWriter writer;

    public ContentIndexer(String indexDir) throws IOException {

        // create the index
        if (writer == null) {
            writer = new IndexWriter(FSDirectory.open(new File(indexDir)),
                    new IndexWriterConfig(Version.LUCENE_36,
                            new StandardAnalyzer(Version.LUCENE_36)));
        }
    }

    public void flushDisk() {
        try {
            writer.deleteAll();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Edited -- Updated Answer

public class Test {
    private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing";

    public static void main(String[] args) {

        try {
            IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,
                    new StandardAnalyzer(Version.LUCENE_36));
            conf.setOpenMode(OpenMode.CREATE);
            Directory directory = FSDirectory.open(new File(INDEX_DIR));

            IndexWriter indexWriter  = new IndexWriter(directory, conf);
            indexWriter.deleteAll();
            indexWriter.commit();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85

2 Answers2

9

The simplest way is to open IndexWriter with a CREATE mode (via indexWriterConfig.setOpenMode(...)). This removes all existing index files in the given directory.

For older versions IndexWriter constructor also has a special boolean create flag which does the same thing.

mindas
  • 26,463
  • 15
  • 97
  • 154
  • 1
    Ok i figured it now.. after the deleteAll() , indexWriter.commit(); should be added..Then it worked like a charm...I have addeed the answer along with the question – Amila Iddamalgoda Jul 14 '14 at 10:33
  • You should probably add your answer as an answer and accept it. – mindas Jul 14 '14 at 11:03
  • You were the one who told me the steps ,first place.. so u better add it to your answer!! :) – Amila Iddamalgoda Jul 14 '14 at 11:05
  • Thanks. this is great. However, I see each time the prefix of the names of the files gets incremented by 1 (_0.cfe, _1.cfe, _2.cfe etc). What if I want the file names to be prefixed by 0 each time? – Asif Iqbal Jun 09 '16 at 21:31
  • @AsifIqbal you can't control file names, this is internal to Lucene. – mindas Jun 13 '16 at 09:56
  • This solution is a lot more elegant than the `indexWriter.deleteAll(); indexWriter.commit();` suggestion in the question. – Thibstars Feb 06 '20 at 09:36
7

You can use two options :

  1. You can call the delete all method of the writer

    indexWriter.DeleteAll();

  2. You can create a new indexWriter with the create flag set to true ( open mode= created)

    new IndexWriter(_luceneDirectory, _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70