How to Delete a Document by Id in Elasticsearch using NodeJS

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re interacting with Elasticsearch using a NodeJS application, there may be occasions when you need to delete a specific document. Fortunately, it’s easy to use the npm Elasticsearch module to communicate with Elasticsearch and issue this command. In this tutorial, we’ll learn how to delete a document by id in Elasticsearch using NodeJS. If you’d prefer to skip the explanations and dive into the sample code, feel free to skip ahead to Just the Code.

Prerequisites

Before we attempt to delete a document by ID in Elasticsearch using NodeJS, it’s important to confirm that a few prerequisites are in place. For this task, the system requirements are minimal: NodeJS needs to be installed, Elasticsearch needs to be installed and running, and a NodeJS application needs to be connected to Elasticsearch.

Note: In our example, we’re assuming that Elasticsearch is running locally on the default port; therefore, you’ll see us communicate with Elasticsearch at "localhost:9200". If your installation of Elasticsearch is running on a different server, your calls to it will take a slightly different format: "YOURDOMAIN.com:9200".

Use the delete function

Let’s look at an example of deleting a document by ID using NodeJS. For this example, we’ll create a sample index called store, which represents a small grocery store. Our store index contains the type products which lists all of the store’s products. We’ll keep our dataset simple by including just a handful of products and a small number of fields: id, price, quantity, and department. The JSON shown below can be used to create our dataset:

idnamepricequantitydepartment
1Multi-Grain Cereal4.994Packaged Foods
21lb Ground Beef3.9929Meat and Seafood
3Dozen Apples2.4912Produce
4Chocolate Bar1.292Packaged FoodsCheckout
51 Gallon Milk3.2916Dairy
60.5lb Jumbo Shrimp5.2912Meat and Seafood
7Wheat Bread1.295Bakery
8Pepperoni Pizza2.995Frozen
912 Pack Cola5.296Packaged Foods
10Lime Juice0.9920Produce
1112 Pack Cherry Cola5.595Packaged Foods
121 Gallon Soy Milk3.3910Dairy
131 Gallon Vanilla Soy Milk3.499Dairy
141 Gallon Orange Juice3.294Juice

The following code contains the mapping:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"mappings": {
"products": {
"properties" : {
"name": { "type": "text"},
"price": { "type": "double"},
"quantity": { "type": "integer"},
"department": { "type": "keyword"}
}
}
}
}
'

Let’s say we want to delete the “Lime Juice” product, which has an id of “10”, from this index. We’ll be adding code to accomplish this to our main application file: index.js. In this file, we connect to Elasticsearch with the following code:

1
2
3
4
5
var elasticsearch = require("elasticsearch");

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

We’ll call the delete function on our client, specifying the product id “10”, with the following code:

1
2
3
4
5
await client.delete({
index: "store",
type: "products",
id: "10"
});

Let’s take a closer look at what’s happening in this code. We called the delete function on our client, supplying the various parameters needed to delete a single document: the index, the type, and the id.

The delete function definition is as follows:

1
client.delete([params, [callback]]);

Since our example showed a fairly simple case of deleting a document by id, it doesn’t illustrate the full set of parameters that are available for use. For more information on these parameters, consult Elasticsearch’s documentation.

It would be helpful to know whether our code worked, so let’s add a callback to our code to verify that the delete occurred successfully:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var elasticsearch = require("elasticsearch");

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

client
.delete({
index: "store",
type: "products",
id: "10"
})
.then(
function(resp) {
console.log(resp);
},
function(err) {
console.trace(err.message);
}
);

At this point, we can run our Node application from the terminal like this:

1
$ node index.js

The response you receive will look something like this:

1
2
3
4
5
6
7
8
{ _index: 'store',
_type: 'products',
_id: '10',
_version: 2,
result: 'deleted',
_shards: { total: 2, successful: 1, failed: 0 },
_seq_no: 5,
_primary_term: 1 }

You can see from the successful: 1 response that our deletion was a success. If you need further confirmation, try querying your index for “Lime Juice”, and you’ll see that it’s no longer there.

Conclusion

Deleting a document is permanent, so it’s important to understand how to execute the operation correctly. In this tutorial, we explained how to delete a document by id within a NodeJS application. With the step-by-step instructions in this guide, you’ll be ready to handle any delete tasks needed in your application. For information on all the delete options available in the API, see Elasticsearch’s documentation.

Just the Code

If you’re already comfortable with the concepts explained in this tutorial, here’s all the code you’ll need to delete a document by id in Elasticsearch using NodeJS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var elasticsearch = require("elasticsearch");

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

client
.delete({
index: "store",
type: "products",
id: "10"
})
.then(
function(resp) {
console.log(resp);
},
function(err) {
console.trace(err.message);
}
);

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

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.