---
title: "Delete search index documents by exact field value in C#"
url: https://recursiveai.org/oneindex/cookbook/delete-indexed-documents-by-field-value/
description: "Use OneIndex DeleteByTerm to remove documents with an exact keyword value, commit the deletion, and verify the remaining documents in a new snapshot."
version: "0.4.0-preview.2"
---

# Delete search index documents by exact field value in C#

**OneIndex** is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneIndex) · [GitHub](https://github.com/recursive-ai-oss/oneindex-csharp) · [Docs](https://recursiveai.org/oneindex/docs/) · [Download example (.zip)](https://recursiveai.org/oneindex/cookbook/delete-indexed-documents-by-field-value/delete-indexed-documents-by-field-value.zip)

Use `DeleteByTerm` to remove `draft` from the index but keep `draft-review`.

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneindex/cookbook/delete-indexed-documents-by-field-value/delete-indexed-documents-by-field-value.zip) and `dotnet run`.

```text
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:

```csharp
int deleted = writer.DeleteByTerm("status", "draft-review");
```

```text
Deleted: 1
draft: 2
draft-review: 0
```

## Related recipes

- [Preview uncommitted search edits](https://recursiveai.org/oneindex/cookbook/preview-uncommitted-search-edits/)
- [Rebuild a search index](https://recursiveai.org/oneindex/cookbook/rebuild-search-index/)

## 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](https://recursiveai.org/oneindex/docs/segments/#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](https://recursiveai.org/oneindex/docs/segments/#snapshots-and-file-lifetime) explain the lifetime of those files.
