# Deleting and recreating index sometimes gives 400 Bad Request error

**URL:** <https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562>\
**Category:** Elasticsearch\
**Created:** [November 12, 2019, 4:56pm UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562 "2019-11-12T16:56:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amkoehler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amkoehler/32/57559_2.png) [@amkoehler](https://discuss.elastic.co/u/amkoehler)\
**Post date:** [November 12, 2019, 4:56pm UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/1 "2019-11-12T16:56:13Z")

</div>

I'm writing a script using the elasticsearch node JS API for bulk adding indices. Some of these indices may already exist, so I first do a check to see if the index already exists and delete it prior to recreating the index. When this situation occurs, the delete succeeds but when creating the index I often get a `400 Bad Request` error on the subsequent index creation.

Code:

```auto
function createIndex() {
  // Check if index exists
  const exists = await client.indices.exists({
    index: 'foo-index',
  }),

  if (exists.body === true) {
    // Delete index if it exists
    const deleteResult = await client.indices.delete({
      index,
    });
  }

  // Re-create index 
  // This often throws error if the delete had just occurred
  const createResult = await client.indices.create({
    index: 'foo-index',
    body: { ... },
  });
}

```

Error message:

```json
{
  "name": "ResponseError",
  "meta": {
    "body": "400 Bad Request",
    "statusCode": 400,
    "headers": {
      "content-type": "text/plain; charset=utf-8",
      "connection": "close"
    },
    "warnings": null,
    "meta": {
      "context": null,
      "request": {
        "params": {
          "method": "PUT",
          "path": "/foo-index",
          "body": {...},
          "querystring": "",
          "headers": {
            "User-Agent": "elasticsearch-js/7.4.0 (linux 4.4.0-18362-Microsoft-x64; Node.js v10.14.2)",
            "Content-Type": "application/json",
            "Content-Encoding": "gzip",
            "Accept-Encoding": "gzip,deflate"
          },
          "timeout": 30000
        },
        "options": {
          "warnings": null
        },
        "id": 21
      },
      "name": "elasticsearch-js",
      "connection": {...},
      "attempts": 0,
      "aborted": false
    }
  }
}

```

I thought this might be a timing issue, so I added a 1500ms delay between the delete and the index creation. That didn't seem to make a difference. I ran a separate script that did the bulk delete prior to running this script, meaning the delete would always get skipped in this script, and everything ran fine. That's not a great long term solution for us so I'd still like to figure out why this isn't working. Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [November 12, 2019, 7:01pm UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/2 "2019-11-12T19:01:34Z")

</div>

As you may have already surmised, attempting to create an index that already exists returns an HTTP 400. So, it seems that either the there are deletes failing, or you aren't even attempting to delete all the indices you think you are. (In the provided code, it's clear that the second scenario isn't happening, as it's a single index name hard coded; by the same token, it's clear that this isn't the actual code, so in the actual code, it's still possible.)

I was start with adding more checks and visibility in your code. Check the response from the attempted deletion each time. Add a data structure where you store index names that have been successfully deleted, then check whether index names you try to delete are contained in that data structure.

---

<div class="post-metadata">

**Author:** ![amkoehler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amkoehler/32/57559_2.png) [@amkoehler](https://discuss.elastic.co/u/amkoehler)\
**Post date:** [November 12, 2019, 7:35pm UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/3 "2019-11-12T19:35:15Z")

</div>

Thanks Glen. That's a good thought on the added checks and I'll get going with those.

The 400 error kinda threw me because I've ran into index already existing before and it'll return `index_already_exists_exception` in the error response body. That isn't the case with these errors which made me think it was something else.

---

<div class="post-metadata">

**Author:** ![amkoehler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amkoehler/32/57559_2.png) [@amkoehler](https://discuss.elastic.co/u/amkoehler)\
**Post date:** [November 15, 2019, 7:19pm UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/4 "2019-11-15T19:19:15Z")

</div>

I added a check to ensure each deletion was acknowledged before continuing. All of my delete operations are running successfully, but the index recreation is still erroring out inconsistently with a `400 bad request`. The error message body does _not_ contain any info about the index already existing. It's just a plain 400 with no additional info.

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [November 16, 2019, 1:12am UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/5 "2019-11-16T01:12:25Z")

</div>

Can you provide the actual code used to repro and the application logs from a run where this happens?

---

<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 14, 2019, 1:12am UTC](https://discuss.elastic.co/t/deleting-and-recreating-index-sometimes-gives-400-bad-request-error/207562/6 "2019-12-14T01:12:37Z")

</div>

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