# Deleted documents reappearing in searches, how to write unit tests for this?

**URL:** <https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396>\
**Category:** Elasticsearch\
**Created:** [May 10, 2022, 11:24pm UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396 "2022-05-10T23:24:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![catmanjan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catmanjan/32/77810_2.png) [@catmanjan](https://discuss.elastic.co/u/catmanjan)\
**Post date:** [May 10, 2022, 11:24pm UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396/1 "2022-05-10T23:24:09Z")

</div>

Hello I have a simple CRUD app that reads and writes to Elasticsearch, I am up to writing some unit tests for the app, which involves creating dummy data, checking I can search for the data and then deleting the data.

I have noticed that sometimes my tests are failing, as data which was deleted in a previous test sometimes reappears.

I am able to replicate the issue querying Elasticsearch directly:

1. run my tests (created,search,delete)
2. in the browser go to [http://localhost:9200/MYINDEXNAME/\_search](http://localhost:9200/MYINDEXNAME/_search) and refresh the page over and over
3. at this stage I am expecting the \_search to return zero results, which it usually does, but sometimes it will return 1 or 2 results which are previously deleted dummy data

I am running Elasticsearch locally on Windows and just used the stock standard ZIP package, no modifications made to the configuration

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 11, 2022, 1:32am UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396/2 "2022-05-11T01:32:53Z")

</div>

It'd be useful if you shared your code. Once a document has been deleted (and has been ack'd), then it's not available for Elasticsearch to retrieve. So something else is happening.

---

<div class="post-metadata">

**Author:** ![catmanjan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catmanjan/32/77810_2.png) [@catmanjan](https://discuss.elastic.co/u/catmanjan)\
**Post date:** [May 11, 2022, 1:44am UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396/3 "2022-05-11T01:44:02Z")

</div>

Set up:

```auto
            var settings = new ConnectionSettings(new Uri(Configuration["ElasticsearchConfiguration:Uri"]))
                .BasicAuthentication(Configuration["ElasticsearchConfiguration:Username"], Configuration["ElasticsearchConfiguration:Password"])
                .DefaultMappingFor<ElasticDocument>(m => m.IndexName(documentIndexName));

            client = new ElasticClient(settings);

            client.Indices.Create(documentIndexName, c => c
                .Map<ElasticDocument>(m => m.AutoMap()));

```

Here is how I add to my index:

```auto
            var elasticDocument = new ElasticDocument()
            {
                Uid = document.Id
            };

            client.Index(elasticDocument, o => o);

```

and delete:

```auto
            client.Delete<ElasticDocument>(documentId);

```

the actual model:

```auto
        [ElasticsearchType(IdProperty = nameof(Uid))]
        private class ElasticDocument
        {
            [Keyword(Name = "uid")]
            public string Uid { get; set; }
        }

```

---

<div class="post-metadata">

**Author:** ![catmanjan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catmanjan/32/77810_2.png) [@catmanjan](https://discuss.elastic.co/u/catmanjan)\
**Post date:** [May 11, 2022, 1:48am UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396/4 "2022-05-11T01:48:02Z")

</div>

Oh, and the search thats returning unexpected results sometimes:

```auto
            var searchResponse = client.Search<ElasticDocument>(q => q
                .Query(rq => rq
                    .Bool(b => b
                        .Must(m => m.MatchAll())));

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 8, 2022, 1:48am UTC](https://discuss.elastic.co/t/deleted-documents-reappearing-in-searches-how-to-write-unit-tests-for-this/304396/5 "2022-06-08T01:48:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
