How to Delete Multiple Indices in Elasticsearch with NodeJS

Introduction

Most Elasticsearch users have come across the situation where they need to delete a number of indices at once. You might have created several indices to test out features, but you don’t them anymore and would like to clean up your data. You might have changed your overall data structure and find that you’re no longer using some indices. Regardless of the exact circumstances, it’s easy to remove multiple Elasticsearch indices with some Javascript running on NodeJS. In this step-by-step guide, we’ll show you how to delete several indices 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.

How to Delete Multiple Indices

We love to show by example. In this example we have a indices for 3 small grocery stores ( 1 index per store). These indices are named store1, store2, store3. Now et’s take a look at how simple deleting these indices would be using the delete index API. The Javascript code is fairly straightforward so let’s take a look at it and then go over the details after:

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);
});

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])

We could have also deleted the same three indices using a wildcard expression like this:

File: deleteMutiple.js

var elasticsearch = require("elasticsearch");

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

/* Delete multiple indices with a wildcard expression */
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);
});

Remember that when you delete indices, you’re actually deleting an entire database table full of documents, so it’s important to use these commands 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 function, consult Elasticsearch documentation.

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 multiple indices using Javascript running on NodeJS.

Just the Code

If you’re already familiar with the concepts of deleting indices and just want to see the code, here’s all the code we used to demonstrate how to delete multiple indices using Javascript:

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);
});

File: deleteMutipleWithWildcard.js

var elasticsearch = require("elasticsearch");

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

/* Delete multiple indices with a wildcard expression */
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);
});

Pilot the ObjectRocket Platform Free!

Try Fully-Managed Redis,
MongoDB & Elasticsearch

Get Started

OR

Try CockroachDB
in Beta

Get Started

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.