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 needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-search -- dnx GroupDocs.Search.Mcp --yes
Folders, licensing and more →

GroupDocs.Search has the ability to remove individual documents from the index that are indexed from a stream or structure. To remove documents indexed by a file system path, see Deleting indexed paths.

The following example shows how to remove documents from an index by document key.

// Implementing document loader
private static class DocumentLoader implements IDocumentLoader {
    private final String filePath;
    private final String documentKey;

    public DocumentLoader(String filePath) {
        this.filePath = filePath;
        documentKey = FilenameUtils.getName(filePath);
    }

    public final String getDocumentKey() {
        return documentKey;
    }

    public final void closeDocument() {
    }

    public final Document loadDocument() {
        try {
            String extension = "." + FilenameUtils.getExtension(filePath);
            Path path = Paths.get(filePath);
            byte[] buffer = Files.readAllBytes(path);
            ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
            Document document = Document.createFromStream(documentKey, new Date(System.currentTimeMillis()), extension, stream);
            return document;
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

...

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.getDocumentKey(), 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.getDocumentKey() };
DeleteResult deleteResult = index.delete(new UpdateOptions(), documentKeys);

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.