Delete all index data/files in disk using Apache Lucene?
flush, java, lucene, search-engine
Solution
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.
Problem
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(); } } } ```