# C# How simply delete by id or query with c#

**URL:** <https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405>\
**Category:** Elasticsearch\
**Created:** [January 31, 2017, 6:02pm UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405 "2017-01-31T18:02:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [January 31, 2017, 6:02pm UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/1 "2017-01-31T18:02:31Z")

</div>

Hello

Not a question this time, but just a share of code.  
But if someone have better, I take 🙂

My problem is how delete document with query in [Elasticsearch.NET](http://Elasticsearch.NET)  
I read a lot of subjets, but I definitely fail to delete with c# API  
So I use a tricky solution 🙂  
Use c# API to do a search request, use http delete to delete document.

```
private void CleanAutomatesName()
{
    List<string> result = new List<string>();
    var query = @"{
                ""size"" : 1000,
                ""filter"": {
                    ""term"": {
                        ""automate"":""1""
                    }
                 }
              }";
    var tests = client.LowLevel.Search<Test>("logs-", query);
    var str = Encoding.Default.GetString(tests.ResponseBodyInBytes);
    dynamic data = JObject.Parse(str);
    if (data["hits"].total > 0)
    {
        var logs = data["hits"]["hits"];
        foreach (var log in logs)
        {
            Console.WriteLine(log._id);
            Delete("http:// ******my_ip:my_port***** /logs-/logs-/" + log._id +"/");
        }
    }
}

public virtual void Delete(string url)
{
    Delete(url, "application/x-www-form-urlencoded");
}

private void Delete(string url, string contentType)
{
    var request = WebRequest.Create(url);
    request.Method = "DELETE";
    request.Timeout = 5000;
    request.ContentType = contentType;
    try {
        var response = request.GetResponse();
        response.GetResponseStream();
        response.Close();
    }
    catch (Exception) { }
}

```

The performance are horrible, but it works 🙌  
If someone have a good version with API, I am really interested  
So far, I try some code like this:

```
var d1remove = client.DeleteByQuery<Test>("logs-", "logs-", q => q.Index(Indices.Parse("logs-")).Type(Types.Parse("logs-")).Query(rq => rq.Term(f => f._id, (string)log._id)));

```

or

```
QueryContainer qcremove = null;
qcremove &= new TermQuery { Field = "_id", Value = log._id };
var deleteRequest = new DeleteByQueryRequest(Indices.Parse("logs-"), Types.Parse("logs-"));
deleteRequest.Query = qcremove;
var d1remove = client.DeleteByQuery(deleteRequest);

```

But with no result. 😥

Regards

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [January 31, 2017, 11:14pm UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/2 "2017-01-31T23:14:38Z")

</div>

What does your mapping look like for `logs-` document type in `logs-` index?

---

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [February 1, 2017, 3:04pm UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/3 "2017-02-01T15:04:09Z")

</div>

Because I just have logs 🙂  
So I have juste one level of document and index . It is the raw data  
I create new index after with script

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 2, 2017, 1:23am UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/4 "2017-02-02T01:23:40Z")

</div>

what does the mapping looks like i.e. what is returned for

```auto
GET /logs-/_mapping/logs-

```

---

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [February 2, 2017, 10:41am UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/5 "2017-02-02T10:41:10Z")

</div>

```
{
"logs-": {
    "mappings": {
        "logs-": {
            "properties": {
                "@timestamp": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
             },
            "@version": {
                "type": "string"
            },
            "AckServerLoopId": {
                "type": "string"
            },
            "AckServerMic": {
                "type": "string"
            },
            [...] more than dozen of other fields

```

Why this question? 🙂

---

<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:** [March 2, 2017, 10:41am UTC](https://discuss.elastic.co/t/c-how-simply-delete-by-id-or-query-with-c/73405/6 "2017-03-02T10:41:12Z")

</div>

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