Method DeleteDocument
DeleteDocument(string, string, CancellationToken)
Deletes all sections associated with a specific document from a data source.
public bool DeleteDocument(string documentId, string dataSourceIdentifier, CancellationToken cancellationToken = default)
Parameters
documentIdstringThe unique identifier of the document to delete. This corresponds to the document ID assigned during import via DocumentRag.DocumentMetadata.
dataSourceIdentifierstringThe identifier of the data source containing the document.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- bool
trueif the data source was found and the deletion was attempted;falseif no matching data source exists.
Examples
// First, import a document with an explicit ID
var attachment = Attachment.FromFile("report.pdf");
var metadata = new DocumentMetadata(attachment, id: "report-2024-q4");
docRag.ImportDocument(attachment, metadata, "financial-reports");
// Later, delete the document using the same ID
bool deleted = docRag.DeleteDocument("report-2024-q4", "financial-reports");
if (deleted)
{
Console.WriteLine("Document removed from index.");
}
Remarks
This method removes all partitions that were created when the specified document was imported. Documents are identified by their unique document ID stored in partition metadata during import.
When using a IVectorStore, deletions are performed directly against the collection. For in-memory storage, sections are removed from the data source object.
Exceptions
- ArgumentException
Thrown if
documentIdordataSourceIdentifierisnull, empty, or whitespace.