GroupDocs.Search has the ability to remove individual documents from the index that are indexed from a stream or structure. Such documents are identified by their document keys. To remove documents indexed by a file system path, see Delete indexed paths.
Deleting documents does not rewrite the index. Deleted documents are excluded from search results immediately, but their data remain in the index files until the index is optimized. Therefore, after deleting a large number of documents, call the Optimize method to physically remove them and free up disk space. The DeleteResult object returned by the Delete method contains the total, successful and failed counts, the results for individual items and the error messages.
The following example shows how to remove documents from an index by document key.
C#
// Implementing document loaderpublicclassDocumentLoader:IDocumentLoader{privatereadonlystringfilePath;privatereadonlystringdocumentKey;publicDocumentLoader(stringfilePath){this.filePath=filePath;documentKey=Path.GetFileName(filePath);}publicstringDocumentKey{get{returndocumentKey;}}publicvoidCloseDocument(){}publicDocumentLoadDocument(){stringextension=Path.GetExtension(filePath);byte[]buffer=File.ReadAllBytes(filePath);Streamstream=newMemoryStream(buffer);Documentdocument=Document.CreateFromStream(documentKey,DateTime.Now,extension,stream);returndocument;}}...stringfilePath=@"c:\MyDocuments\SomeDocument.pdf";stringindexFolder=@"c:\MyIndex\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing the document from streamDocumentLoaderdocumentLoader=newDocumentLoader(filePath);Documentdocument=Document.CreateLazy(DocumentSourceKind.Stream,documentLoader.DocumentKey,documentLoader);Document[]documents=newDocument[]{document};index.Add(documents,newIndexingOptions());// Searching in the indexSearchResultsearchResult=index.Search("Einstein");// Deleting indexed document from the indexstring[]documentKeys=newstring[]{documentLoader.DocumentKey};DeleteResultdeleteResult=index.Delete(newUpdateOptions(),documentKeys);Console.WriteLine("Deleted documents: "+deleteResult.SuccessCount);// Physically removing the deleted documents from the index filesindex.Optimize();
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: