Delete indexed documents

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

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 loader
public class DocumentLoader : IDocumentLoader
{
    private readonly string filePath;
    private readonly string documentKey;

    public DocumentLoader(string filePath)
    {
        this.filePath = filePath;
        documentKey = Path.GetFileName(filePath);
    }

    public string DocumentKey
    {
        get { return documentKey; }
    }

    public void CloseDocument()
    {
    }

    public Document LoadDocument()
    {
        string extension = Path.GetExtension(filePath);
        byte[] buffer = File.ReadAllBytes(filePath);
        Stream stream = new MemoryStream(buffer);
        Document document = Document.CreateFromStream(documentKey, DateTime.Now, extension, stream);
        return document;
    }
}

...

string filePath = @"c:\MyDocuments\SomeDocument.pdf";
string indexFolder = @"c:\MyIndex\";

// Creating an index in the specified folder
Index index = new Index(indexFolder);

// Indexing the document from stream
DocumentLoader documentLoader = new DocumentLoader(filePath);
Document document = Document.CreateLazy(DocumentSourceKind.Stream, documentLoader.DocumentKey, documentLoader);
Document[] documents = new Document[] { document };
index.Add(documents, new IndexingOptions());

// Searching in the index
SearchResult searchResult = index.Search("Einstein");

// Deleting indexed document from the index
string[] documentKeys = new string[] { documentLoader.DocumentKey };
DeleteResult deleteResult = index.Delete(new UpdateOptions(), documentKeys);
Console.WriteLine("Deleted documents: " + deleteResult.SuccessCount);

// Physically removing the deleted documents from the index files
index.Optimize();

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.