Luckily, with Elasticsearch, when data become redundant, all you need to do is access a tool to perform requests and transfer data over the network.
This quick guide will show you how to use the mighty Elasticsearch API to delete documents and indices.
NOTE: We assume you have Elasticsearch running on your system and that you have a tool for making requests such as cURL. We also provide raw Kibana requests if you are using the Kibana Console (recommended).
How to List Index?
If you want to delete and index in Elasticsearch, you first need to verify it exists before sending the DELETE request.
If you try to delete a non-existing index, you will get an error, similar to the one shown below:
For cURL command:
Deleting an index will give an error as:
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [this_index_does_not_exist]",
"index_uuid" : "_na_",
"resource.type" : "index_or_alias",
"resource.id" : "this_index_does_not_exist",
"index" : "this_index_does_not_exist"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [this_index_does_not_exist]",
"index_uuid" : "_na_",
"resource.type" : "index_or_alias",
"resource.id" : "this_index_does_not_exist",
"index" : "this_index_does_not_exist"
},
"status" : 404
}
There are various ways to check if an index exists; the best is to list its name. For example, you can use wildcards to match a specific name.
The example request below lists indices with names te*
The cURL command is:
This command should return all the indices matching that specific pattern, allowing you to remember only the partial name of the index you wish to remove.
"temp" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "temp",
"creation_date" : "1611180802266",
"number_of_replicas" : "1",
"uuid" : "c7dOH6MQQUmHM2MKJ73ekw",
"version" : {
"created" : "7100299"
}
}
}
},
"temp_1" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "temp_1",
"creation_date" : "1611180811874",
"number_of_replicas" : "1",
"uuid" : "pq1UUR2XTZS3xfs6Hxr4gg",
"version" : {
"created" : "7100299"
}
}
}
},
"temp_2" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "temp_2",
"creation_date" : "1611180815041",
"number_of_replicas" : "1",
"uuid" : "8NdXWPuBTLe6r4eZ407W9Q",
"version" : {
"created" : "7100299"
}
}
}
}
}
Another way is to add the ignore_unavailable parameter to the request. For example:
For cURL users:
[cc lang="text" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
curl -XDELETE “http://localhost:9200/ignore_me?ignore_unavailable=true”
How to Delete an Index?
Once you have the index you wish to remove from Elasticsearch, use the DELETE request followed by the index name.
The general syntax is:
The index name can be a specific index or a wildcard that selects a group of indices. Ensure to use wildcards correctly; otherwise, you might remove the wrong indices.
NOTE: Deleting Elasticsearch indices using aliases is disallowed.
Consider the example request below that removes the temp_1 index:
For cURL command:
Executing this command should respond with a JSON object, indicating the successful removal of the index.
“acknowledged”: true
}
Elasticsearch is smart enough to know that you can remove indices accidentally. Therefore, you can set what types of wildcard expressions are allowed.
These type of wildcards expressions include:
- All: Includes all indices, including open, closed, and hidden (starting with)
- Open: Includes open indices only
- Closed: Includes closed indices only
- None: No wildcard expressions allowed.
Conclusion
For this quick and simple guide, we discussed the process of using Elasticsearch to delete indices from a cluster. We also discussed simple ways you can implement to avoid errors for indices that do not exist.
Thank you for reading.