Close

How to delete data from Elastisearch

Posted by: AJ Welch

One of the unique design features of Elasticsearch is that, unlike most traditional systems or databases, all tasks such as connecting to and manipulating Elasticsearch are performed using a REST API, meaning that nearly every query or command executed on your Elasticsearch node is a simple HTTP request to a particular URL.

Depending on the HTTP verb sent and the URL that verb it is sent to, Elasticsearch can perform a huge variety of actions on the node or even the cluster.


The Elasticsearch REST API URL structure


At the most basic level, to execute a command in Elasticsearch, you’ll need to send an HTTP verb to the URL of your Elasticsearch node. For development, typically this is localhost:9200.

In most cases, the simplest method for sending a request to the REST API of Elasticsearch is through the useful command-line tool, cURL, which is a simple tool used to transfer nearly any kind of Internet data.

For example, to list all indices, you may execute the following curl command from the shell prompt of your development server (as indicated in the official documentation:

$ curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted bookstore.size pri.bookstore.size

Here we’re accessing the cat API (indicated by the leading _ underscore) and viewing the indices, which shows a cross-section of each index in the cluster.

Indices, types, documents, and properties

With an example in place, we can explore in more detail the specific structure of Elasticsearch REST APIs, which are most often going to consist of three structured components, the index, the type, and the document:

localhost:9200/index/type/document

The index is the parent structure and is most simply thought of as a database that houses many types. And index can represent any concept, but often will represent a whole system of components such as a shop or a bookstore.

Types are contained in an index and are similar to database tables, with each type representing a collection of similar objects (like shirt or book).

Finally, the document is a single instance or representation of an object of the parent type. Thus, the book “The Hobbit” may exist as a book type in the index named bookstore.

Deleting data from Elasticsearch


With the basic REST API syntax out of the way, we can explore how to perform specific actions like deleting data.

Delete a single document

Taking our basic syntax as seen above, we need to use curl and send the DELETE HTTP verb, using the -XDELETE option:

$ curl -XDELETE 'localhost:9200/index/type/document'

For example, to delete our aforementioned book document, we might use the following command:

$ curl -XDELETE 'localhost:9200/bookstore/book/1'

This will delete the document with an ID of 1 from the book type that is within the bookstore index.

Delete a type

As you might guess, with the syntax only broadening slightly, we’re able to remove an entire type. Here we’re deleting the book type:

$ curl -XDELETE 'localhost:9200/bookstore/book'

Delete an index

Lastly, if we wish to delete an entire index, this can be done using the same syntax as before:

$ curl -XDELETE 'localhost:9200/bookstore'