Use DeleteByTerm to remove draft from the index but keep draft-review.
dotnet add package RecursiveAI.OneIndex --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneIndex.Index; using OneIndex.Search; using OneIndex.Storage; var store = new MemoryStorage(); using var writer = OpenWithSamples(store); int deleted = writer.DeleteByTerm( "status", "draft"); writer.Commit(); Console.WriteLine($"Deleted: {deleted}"); using var view = IndexSnapshot.Open(store); var search = SnapshotSearch.CreateSearcher( view); string[] statuses = ["draft", "draft-review"]; foreach (var status in statuses) { var query = new TermQuery( "status", status); var hits = search.Search(query, 10); Console.WriteLine( $"{status}: {hits.TotalHits}"); } static IndexWriter OpenWithSamples( MemoryStorage store) { var schema = new SchemaBuilder() .Keyword("status").Build(); var writer = IndexWriter.Open(store, schema); writer.Add(new Document() .Add("status", "draft")); writer.Add(new Document() .Add("status", "draft")); writer.Add(new Document() .Add("status", "draft-review")); writer.Commit(); return writer; }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Deleted: 2 draft: 0 draft-review: 1
DeleteByTerm returns the number of live documents it marked for deletion. It requires a keyword field: .Keyword("status") indexes the whole value as one exact term. DeleteByTerm throws ArgumentException if the field isn’t a keyword field.
Variations
Delete the longer status instead. Replace the deletion line, keeping the following Commit() and the same two checks:
int deleted = writer.DeleteByTerm("status", "draft-review");Deleted: 1 draft: 2 draft-review: 0
Related recipes
The short answer
Call DeleteByTerm(field, value) on your IndexWriter, commit, then open a fresh IndexSnapshot. Use a keyword field so the indexed value is matched exactly. This removes index documents, not rows in the database or files you originally indexed.
How do you delete multiple documents by one field value?
Pass the shared value to DeleteByTerm once. In this example, two separate documents have status set to draft, and one has draft-review. The latter is a different term, even though it starts with the same letters. The operation is exact: it doesn’t expand a prefix or parse a search expression. If there’s nothing left with that exact value, the returned count is zero. When you know the unique ID rather than a shared field value, use DeleteById on an index with an ID field.
If you’re deleting based on input you haven’t used before, first run a TermQuery on the same field and value to check which documents would be affected. A query on a committed snapshot shows that snapshot’s contents; the writer can also delete matching documents that are still buffered. See the writer lifecycle.
Why do deleted documents still show up in search?
Check that you committed the changes and that the search is using a new snapshot. The writer marks deletions immediately, but they are published at commit. An existing snapshot stays on the commit it originally opened, so searches on that snapshot don’t change underfoot. Open a new snapshot after the commit or refresh a long-lived IndexReader before acquiring its next view. Dispose snapshots you no longer need so they don’t keep old segment files pinned. The snapshot rules explain the lifetime of those files.