{"id":14538,"date":"2022-10-22T14:15:59","date_gmt":"2022-10-22T11:15:59","guid":{"rendered":"https:\/\/kifarunix.com\/?p=14538"},"modified":"2024-03-09T22:54:57","modified_gmt":"2024-03-09T19:54:57","slug":"delete-specific-records-from-elasticsearch-index","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/delete-specific-records-from-elasticsearch-index\/","title":{"rendered":"Delete Specific Records from Elasticsearch Index"},"content":{"rendered":"\n
This is a simple tutorial on how to search and delete specific records from Elasticsearch index. Elasticsearch ships with a delete_by_query<\/a> API that enables you to search Elasticsearch index for records that matches a specified query and delete them.<\/p>\n\n\n\n Note that if the security features are enabled on your Elasticsearch cluster, you need to have the the following permissions to use delete_by_query API, you need to delete records.<\/p>\n\n\n\n Consider our demo index, show in the screenshot below with a total of 8646 event records in a period of 3 days<\/p>\n\n\n\n Checking the count from the command line (modsec<\/strong>, is our index);<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n Out of these records, there are some notice<\/strong> events that I do not really want from my index.<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n We have 69 such records;<\/p>\n\n\n\n So, if you know the Elasticsearch field that has the keyword that you want to delete, then it becomes easy to use the delete_by_query API.<\/p>\n\n\n\n For example, to delete the above notice messages, the field is message<\/strong> and the keyword is “:notice<\/strong>“.<\/p>\n\n\n\n You can run the delete_by_query<\/strong> from Kibana > Management > Dev Tools > Console<\/strong> or from the terminal command line.<\/p>\n\n\n\n To delete from Kibana DevTools console, paste the query below and press the play button to execute the query command;<\/p>\n\n\n\n NOTE: modsec*<\/strong> is our index with (wildcard, * to match all indices whose names starts with modsec}.<\/p>\n\n\n\n To search all data streams and indices, omit this parameter or use From command line, you can use a command;<\/p>\n\n\n\n It is also possible to further refine the query, for example, to delete specific event data record from specific host.<\/p>\n\n\n\n Consider the query below;<\/p>\n\n\n\n When executed, it will search for events sent by the host sales.kifarunix.com<\/strong> (field is hostname<\/strong>)and contain source_ip<\/strong>: xx.xx.xx.xx and delete them;<\/p>\n\n\n\n You can run the above command on Kibana dev tools console or from command line.<\/p>\n\n\n\n You can also delete documents with a missing field.<\/p>\n\n\n\n For example, to delete records from the index logstash*<\/strong> sent by the host sales.kifarunix-demo.com and doesnt have a field called status_code<\/strong>, you could use such a delete query;<\/p>\n\n\n\n The search query below give total number of search events.<\/p>\n\n\n\n To delete these events;<\/p>\n\n\n\n You can play around with query. Be careful not to delete your important records!!<\/p>\n\n\n\n Delete by query API<\/a><\/p>\n\n\n\n Elasticsearch Boolean Queries<\/a><\/p>\n\n\n\nDelete Specific Records from Elasticsearch Index<\/h2>\n\n\n\n
Required Permissions<\/h3>\n\n\n\n
\n
read<\/code><\/li>\n\n\n\n
delete<\/code> or
write<\/code><\/li>\n<\/ul>\n\n\n\n
Delete Specific Records from an Index<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
curl -XGET http:\/\/elk.kifarunix-demo.com:9200\/modsec*\/_count?pretty<\/code><\/pre>\n\n\n\n
{\n \"count\" : 8646<\/strong>,\n \"_shards\" : {\n \"total\" : 1,\n \"successful\" : 1,\n \"skipped\" : 0,\n \"failed\" : 0\n }\n}<\/code><\/pre>\n\n\n\n
curl -XGET -H 'Content-Type: application\/json' http:\/\/elk.kifarunix-demo.com:9200\/modsec*\/_count?pretty -d '{\n\"query\" : {\n\"match\" : { \"message\": \"notice\" }\n}\n}'<\/code><\/pre>\n\n\n\n
{\n \"count\" : 69,\n \"_shards\" : {\n \"total\" : 1,\n \"successful\" : 1,\n \"skipped\" : 0,\n \"failed\" : 0\n }\n}<\/code><\/pre>\n\n\n\n
*<\/code> or
_all<\/code>.<\/p>\n\n\n\n
\nPOST \/modsec*\/_delete_by_query\n{\n \"query\": {\n \"bool\": {\n \"must\": [\n {\n \"match\": {\n \"message\": \":notice\"\n }\n }\n ] \n }\n }\n}\n<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
\ncurl -XPOST \"http:\/\/elk.kifarunix-demo.com:9200\/modsec*\/_delete_by_query?pretty\" -H 'Content-Type: application\/json' -d '{\n \"query\": {\n \"bool\": {\n \"must\": [\n {\n \"match\": {\n \"message\": \":notice\"\n }\n }\n ] \n }\n }\n}'\n<\/code><\/pre>\n\n\n\n
\nPOST \/test*\/_delete_by_query\n{\n \"query\": {\n \"bool\": {\n \"must\": [\n {\n \"match\": {\n \"hostname\": \"sales.kifarunix.com\"\n }\n },\n {\n \"match\": {\n \"source_ip\": \"xx.xx.xx.xx\"\n }\n }\n ]\n }\n }\n}\n<\/code><\/pre>\n\n\n\n
\nPOST \/logstash*\/_search\n{\n \"query\": {\n \"bool\" : {\n \"must\" : {\n \"match\" : { \"host.name\" : \"sales.kifarunix-demo.com\" }\n },\n \"must_not\" : {\n \"exists\" : { \"field\" : \"status_code\" }\n }\n }\n }\n}\n<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
\nPOST \/logstash*\/_delete_by_query\n{\n \"query\": {\n \"bool\" : {\n \"must\" : {\n \"match\" : { \"host.name\" : \"sales.kifarunix-demo.com\" }\n },\n \"must_not\" : {\n \"exists\" : { \"field\" : \"status_code\" }\n }\n }\n }\n}\n<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Further Readings;<\/h3>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n