How to Delete an Index in Elasticsearch Using NodeJS

Introduction

When you’re working with data in Elasticsearch, there will likely be situations when you need to delete an index. You might have created an index just to test out some feature, but you don’t need to keep it permanently. You might have changed your overall data structure and find that you’re no longer using a particular index, or you may have migrated your data to another system. Regardless of the exact circumstances, it’s easy to remove an Elasticsearch index with some Javascript running on NodeJS. In this step-by-step guide, we’ll show you how to delete an index in Elasticsearch using NodeJS. If you already have experience with NodeJS and Javascript and prefer to dive straight into the sample code, feel free to skip ahead to Just the Code.

Prerequisites

Before we show you how to delete an index using Javascript, it’s important to make sure a few prerequisites are in place. There are only a few of system requirements for this task: NodeJS needs to be installed The elasticsearch npm module installed. A simple npm install elasticsearch should work in most cases. Elasticsearch also needs to be installed and running. * In our example, we have Elasticsearch installed locally using the default port of 9200. If your Elasticsearch installation is running on a different server, you’ll need to modify your javascript syntax accordingly.

Use the delete index API

We love to show by example. In this example we use an index representing a small grocery store called store. Let’s take a look at a simple example of how to delete a single index using the delete index API. In this example, we’ll be deleting an our index called store. The Javascript code is fairly straightforward so let’s take a look at it and then go over the details after:

File: delete.js

var elasticsearch = require("elasticsearch");

var client = new elasticsearch.Client({
  hosts: ["http://localhost:9200"]
});


/* Delete index */
client.indices.delete({
  index: 'store',
}).then(function(resp) {
  console.log("Successful query!");
  console.log(JSON.stringify(resp, null, 4));
}, function(err) {
  console.trace(err.message);
});

First we require the elasticsearch library which you should have downloaded. Then using the elasticsearch object we create an Elasticsearch client that we used to interact with Elasticsearch. Our Elasticsearch instance is running on http:/localhost:9200 but you should modify this to work with your specific situation. Then with the client object we can make a call to delete the index. This is the function defintion to delete an index:

client.indices.delete([params] [, options] [, callback])

There are a number of optional parameters including a callback, but for the purposes of this article we only used the one parameter that really matters, the index.

The code following the delete function call defines what to do in the case that the delete is successful and also what to do in the case that there is an error. If you don’t understand the .then syntax, research Javascript promises.

In the case of a success we log the response using JSON.stringify to make it easy to read and in the case of an error we log the error.

We run the application with NodeJS using the command below:

$ node delete.js

Now let’s see the response:

$ node delete.js
Successful query!
{
    "acknowledged": true
}

You can see that we get a success message which verifies that our index was successfully deleted.

When you delete an index, you’re actually deleting an entire database table full of documents, so it’s important to use this command with care to avoid losing critical data. Fortunately, the Delete Index API is simple to understand and use, so mistakes tend to be uncommon. If you’re unsure about any of the options associated with the delete index command, consult Elasticsearch documentation.

How to delete multiple Indices

The example shown above illustrates how to delete a single index in Elasticsearch, but it’s also possible to delete multiple indices by using wildcard expressions or a list. You can even delete all indices using the “*” wildcard or the keyword _all.

We find that most common use case is to delete a list of indices so the following code shows how to delete three indices (store, store2, store3) in NodeJS.

File: deleteMutiple.js

var elasticsearch = require("elasticsearch");

var client = new elasticsearch.Client({
  hosts: ["http://localhost:9200"]
});

/* Delete multiple indices with an array */
client.indices.delete({
  index: ['store','store2','store3']
}).then(function(resp) {
  console.log("Successful query!");
  console.log(JSON.stringify(resp, null, 4));
}, function(err) {
  console.trace(err.message);
});

We’ve deleted all three indices in a single command by simpling replacing the single string with the index name with a simple array of indices that you want to delete.

If you need to delete all indices or similarly named indices then refer to the Elasticsearch documentation on wildcard expressions * and _all in the delete API.

Conclusion

If you’re managing data in Elasticsearch, you’ll probably need to delete an index at some point. Fortunately, it’s quick and easy to delete an index in Elasticsearch using Javascript running on NodeJS. With the step-by-step instructions described in this tutorial, you’ll have no trouble deleting either a single index or multiple indices.

Just the Code

If you’re already familiar with the concept of deleting an index, here’s all the code you need to delete a single index or multiple indices using Javascript:

To delete a single index: File: delete.js

var elasticsearch = require("elasticsearch");

var client = new elasticsearch.Client({
  hosts: ["http://localhost:9200"]
});


/* Delete index */
client.indices.delete({
  index: 'store',
}).then(function(resp) {
  console.log("Successful query!");
  console.log(JSON.stringify(resp, null, 4));
}, function(err) {
  console.trace(err.message);
});

To delete multiple indices with a wildcard expression: File: deleteMutiple.js

var elasticsearch = require("elasticsearch");

var client = new elasticsearch.Client({
  hosts: ["http://localhost:9200"]
});

/* Delete multiple indices with an array */
client.indices.delete({
  index: ['store','store2','store3']
}).then(function(resp) {
  console.log("Successful query!");
  console.log(JSON.stringify(resp, null, 4));
}, function(err) {
  console.trace(err.message);
});

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.