# \_delete\_by\_query with timestamp

**URL:** <https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157>\
**Category:** Kibana\
**Created:** [November 4, 2022, 5:03am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157 "2022-11-04T05:03:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yiming\_Gong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yiming_gong/32/101378_2.png) [@Yiming\_Gong](https://discuss.elastic.co/u/Yiming_Gong)\
**Post date:** [November 4, 2022, 5:03am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/1 "2022-11-04T05:03:35Z")

</div>

The data I uploaded to Elasticsearch database is like the following

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 35,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "xgWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542H",
          "Module_type" : "osb",
          "case_number" : 33
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "xwWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542H",
          "Module_type" : "e_manual",
          "case_number" : 50
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "yAWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542H",
          "Module_type" : "vha",
          "case_number" : 66
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "yQWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542L",
          "Module_type" : "osb",
          "case_number" : 17
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "ygWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542L",
          "Module_type" : "e_manual",
          "case_number" : 51
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "ywWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD542L",
          "Module_type" : "himalaya",
          "case_number" : 24
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "zAWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD764",
          "Module_type" : "osb",
          "case_number" : 37
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "zQWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD764",
          "Module_type" : "e_manual",
          "case_number" : 61
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "zgWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD764",
          "Module_type" : "himalaya",
          "case_number" : 26
        }
      },
      {
        "_index" : "yiming_test",
        "_type" : "_doc",
        "_id" : "zwWWQIQBSvMJDRAYpLCl",
        "_score" : 1.0,
        "_source" : {
          "timestamp" : "2022-11-04 10:57:41",
          "Vehicle_type" : "CD764",
          "Module_type" : "vha",
          "case_number" : 3
        }
      }
    ]
  }
}

```

The timestamp is in the format of "YY-mm-dd HH:MM:SS"

If I want to fetch the data stored in the index, I use

```auto
GET /yiming_test/_search
{
   "query": {
    "match": {
      "timestamp":"2022-11-04 10:57:41"
      }
    }
}

```

The response is fine, which is exact the same as the screenshot above shows.

However, when I want to delete the data by using

```auto
POST /yiming/_delete_by_query
{
   "query": {
    "match": {
      "timestamp":"2022-11-04 10:57:41"
      }
    }
}

```

No document has been deleted successfully.  
The response I got is as the follows with the response code 200

```auto
{
  "took" : 0,
  "timed_out" : false,
  "total" : 0,
  "deleted" : 0,
  "batches" : 0,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : []
}

```

Could you please tell me what I can do to delete the document based on the timestamp?

Any help is appreciated.

---

<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:** [November 4, 2022, 5:28am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/2 "2022-11-04T05:28:47Z")

</div>

Welcome to our community! 😃

> [@Yiming\_Gong](#):
>
> `"timestamp":"2022-11-04 10:57:41"`

Does not match

> [@Yiming\_Gong](#):
>
> `"timestamp":"2022-11-04 10:57:42"`

There's 1 second difference. And while it's hard to read your screenshot (please just post the code in future!), there doesn't appear to be any "10:57:42" timestamps.

---

<div class="post-metadata">

**Author:** ![Yiming\_Gong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yiming_gong/32/101378_2.png) [@Yiming\_Gong](https://discuss.elastic.co/u/Yiming_Gong)\
**Post date:** [November 4, 2022, 5:33am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/3 "2022-11-04T05:33:44Z")

</div>

sorry, my bad, I changed to

```auto

POST /yiming/_delete_by_query/
{
   "query": {
    "match": {
      "timestamp":"2022-11-04 10:57:41"
      }
    
    }
}

```

still, I got nothing deleted.

```auto
{
  "took" : 0,
  "timed_out" : false,
  "total" : 0,
  "deleted" : 0,
  "batches" : 0,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : []
}

```

By the way I didn't write any code to do these CRUD operations, I just used the dev tools

---

<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:** [November 4, 2022, 5:44am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/4 "2022-11-04T05:44:08Z")

</div>

> [@Yiming\_Gong](#):
>
> By the way I didn't write any code to do these CRUD operations, I just used the dev tools

It's still code. Pictures of text, logs or code are difficult to read, impossible to search and replicate (if it's code), and some people may not be even able to see them 🙂

---

<div class="post-metadata">

**Author:** ![Yiming\_Gong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yiming_gong/32/101378_2.png) [@Yiming\_Gong](https://discuss.elastic.co/u/Yiming_Gong)\
**Post date:** [November 4, 2022, 5:53am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/5 "2022-11-04T05:53:35Z")

</div>

@warkolm , Thank you for your advice, I modified the posts a bit and pasted the code instead of the screenshot. Please help me have a quick check

---

<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:** [November 4, 2022, 5:54am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/6 "2022-11-04T05:54:56Z")

</div>

Thank you, can you show the output from `GET yiming/_mapping` please?

---

<div class="post-metadata">

**Author:** ![Yiming\_Gong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yiming_gong/32/101378_2.png) [@Yiming\_Gong](https://discuss.elastic.co/u/Yiming_Gong)\
**Post date:** [November 4, 2022, 5:56am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/7 "2022-11-04T05:56:14Z")

</div>

Sure, the response is：

```auto
{
  "yiming_test" : {
    "mappings" : {
      "properties" : {
        "Module_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "Vehicle_type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "case_number" : {
          "type" : "long"
        },
        "timestamp" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Yiming\_Gong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yiming_gong/32/101378_2.png) [@Yiming\_Gong](https://discuss.elastic.co/u/Yiming_Gong)\
**Post date:** [November 4, 2022, 7:26am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/8 "2022-11-04T07:26:40Z")

</div>

Any ideas on what went wrong？

---

<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:** [December 2, 2022, 7:26am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/9 "2022-12-02T07:26:52Z")

</div>



---

<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:** [December 30, 2022, 4:21am UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/10 "2022-12-30T04:21:18Z")

</div>



---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [December 30, 2022, 3:46pm UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/11 "2022-12-30T15:46:17Z")

</div>

Sorry for the long delay (I just saw this bumped). No idea what could happen to you but I could index your docs and delete them with your query without issues

```auto
# create index
PUT discuss-318157
{
  "settings": {
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "Module_type": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Vehicle_type": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "case_number": {
        "type": "long"
      },
      "timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

# add data
POST discuss-318157/_bulk
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542H", "Module_type" : "osb", "case_number" : 33 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542H", "Module_type" : "e_manual", "case_number" : 50 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542H", "Module_type" : "vha", "case_number" : 66 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542L", "Module_type" : "osb", "case_number" : 17 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542L", "Module_type" : "e_manual", "case_number" : 51 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD542L", "Module_type" : "himalaya", "case_number" : 24 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD764", "Module_type" : "osb", "case_number" : 37 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD764", "Module_type" : "e_manual", "case_number" : 61 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD764", "Module_type" : "himalaya", "case_number" : 26 }
{"index": {}}
{ "timestamp" : "2022-11-04 10:57:41", "Vehicle_type" : "CD764", "Module_type" : "vha", "case_number" : 3 }

# count all
GET discuss-318157/_count

# count with query
GET discuss-318157/_count
{
   "query": {
    "match": {
      "timestamp":"2022-11-04 10:57:41"
      }
    }
}

# delete with query
POST discuss-318157/_delete_by_query
{
   "query": {
    "match": {
      "timestamp":"2022-11-04 10:57:41"
      }
    }
}

# clean up
DELETE discuss-318157

```

The `delete_by_query` gave me the expected response:

```auto
{
  "took": 16,
  "timed_out": false,
  "total": 10,
  "deleted": 10,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

```

---

<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:** [January 27, 2023, 3:46pm UTC](https://discuss.elastic.co/t/delete-by-query-with-timestamp/318157/12 "2023-01-27T15:46:41Z")

</div>

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