7

I want to delete all the previously created indices. I am using Lucene.net.

I tried the following:

Term term = new Term(); //empty because I want to delete all the indices
IndexReader rdr = IndexReader.Open(_directory);

rdr.DeleteDocuments(term);
rdr.Close();

But I get error. Any idea how to go about it?

sazzad
  • 5,740
  • 6
  • 25
  • 42

4 Answers4

19

The best way to delete an index is to wipe the filesystem directory. However, if you wan't to regenerate the index, the easiest way is to open a new indexwriter with the create parameter as true. It will start a new index deleting the contents of the existing one.

Jokin
  • 4,188
  • 2
  • 31
  • 30
8

although the thread is old i think it's better to give answer.. might be useful for somebody else. deleteAll() method of IndexWriter can be used to delete all documents indexed.

Mandy
  • 582
  • 1
  • 5
  • 13
1

As Jokin said, the easiest was is to delete all of the files within the directory. i.e.;

DirectoryInfo directoryInfo = new DirectoryInfo(@"IndexLocation");
Parallel.ForEach(directoryInfo.GetFiles(), file => {
            file.Delete();
        });
Jeremy Cade
  • 1,351
  • 2
  • 17
  • 28
1

From the Lucene.Net API Doc:

public static IndexReader Open(Directory);

Expert: Returns a read/write IndexReader reading the index in the given Directory, with a custom IndexDeletionPolicy. NOTE: Starting in 3.0 this will return a readOnly IndexReader. Throws CorruptIndexException if the index is corrupt. Throws IOException if there is a low-level IO error.

i guess you should try

IndexReader rdr = IndexReader.Open(_directory, true);
cuh
  • 3,723
  • 4
  • 30
  • 47