Elashticsearch Erase All Documents

  • Category: 電腦相關
  • Last Updated: Friday, 20 January 2017 13:53
  • Published: Friday, 13 January 2017 10:49
  • Written by sam

Quickly Erase All Documents from an ElasticSearch Index

root@wantgooA:/# curator_cli show_indices
root@wantgooA:/# curl -GET http://localhost:9200/_cat/indices
GET /_cat/indices?v
yellow open   logstash-2016.05.07 UfBUsAKdR7CbYKj3Luxvkw   5   1       1530            0      702kb          702kb
yellow open   logstash-2016.12.05 EfG_fl9fTauE9bBdTlNOFA   5   1   18292843            0      8.1gb          8.1gb
yellow open   logstash-2016.10.23 tbvtpEpBTxKLTOPEeh1ftw   5   1      15940            0      4.3mb          4.3mb
yellow open   logstash-2016.03.29 1yGuXTh1QXabRx5xIftVbg   5   1        260            0      274kb          274kb
yellow open   logstash-2016.09.16 AzzX7nRtQ0Gr8SgyflTM0Q   5   1       3925            0      1.7mb          1.7mb
yellow open   logstash-2016.12.06 NU53zD8kR7O4tmBDnI4iCQ   5   1   11222311            0        5gb            5gb
yellow open   logstash-2016.10.27 Mk2dm4t-T0SpTZG0sUx-DA   5   1       5365            0      2.1mb          2.1mb
yellow open   logstash-2016.12.18 PovV_lijQJS93Ho7CiJv5Q   5   1   14439195            0        4gb            4gb
yellow open   index               zwHfPW0lQHuNd9vL9Fxw4Q   5   1          0            0       800b           800b

If you want erase 2016.03.29 index

 

root@wantgooA:/# curl -XDELETE 'http://localhost:9200/logstash-2016.03.29/?pretty=true'
{
  "acknowledged" : true
}

Done

yellow open   logstash-2016.05.07 UfBUsAKdR7CbYKj3Luxvkw   5   1       1530            0      702kb          702kb
yellow open   logstash-2016.12.05 EfG_fl9fTauE9bBdTlNOFA   5   1   18292843            0      8.1gb          8.1gb
yellow open   logstash-2016.10.23 tbvtpEpBTxKLTOPEeh1ftw   5   1      15940            0      4.3mb          4.3mb
yellow open   logstash-2016.09.16 AzzX7nRtQ0Gr8SgyflTM0Q   5   1       3925            0      1.7mb          1.7mb
yellow open   logstash-2016.12.06 NU53zD8kR7O4tmBDnI4iCQ   5   1   11222311            0        5gb            5gb
yellow open   logstash-2016.10.27 Mk2dm4t-T0SpTZG0sUx-DA   5   1       5365            0      2.1mb          2.1mb
yellow open   logstash-2016.12.18 PovV_lijQJS93Ho7CiJv5Q   5   1   14439195            0        4gb            4gb
yellow open   index               zwHfPW0lQHuNd9vL9Fxw4Q   5   1          0            0       800b           800b

Also, you can delete them all at once

curl -XDELETE http://localhost:9200/_all

Or you can use tool curator

reference

Install curator

root@wantgooA:/# vi /etc/apt/sources.list.d/curator.list
deb http://packages.elastic.co/curator/4/debian stable main
root@wantgooA:/# wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
root@wantgooA:/# apt-get update &;& sudo apt-get install elasticsearch-curator

And use it

root@wantgooA:~# vi ~/.curator/curator.yml
###Add in###
client:
 hosts:
    - 127.0.0.1
  port: 9200
  url_prefix:
  use_ssl: False
  certificate:
  client_cert:
  client_key:
  aws_key:
  aws_secret_key:
  aws_region:
  ssl_no_validate: False
  http_auth:
  timeout: 30
  master_only: False

logging:
  loglevel: INFO
  logfile: /var/log/curator.log --> to log file location and name
  logformat: default

set del.yml

actions:
  1:
    action: delete_indices
    options:
      ignore_empty_list: True
      timeout_override:
      continue_if_exception: False
      disable_action: False
    filters:
    - filtertype: pattern
      kind: prefix
      value: logstash-
      exclude:
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 180
      exclude:

And exe it

root@wantgooA:~# curator del.yml
2017-01-13 15:34:32,612 INFO      Preparing Action ID: 1, "delete_indices"
2017-01-13 15:34:32,649 INFO      Trying Action ID: 1, "delete_indices": Delete indices older than 45 days (based on index name), for logstash- prefixed indices. Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly.
2017-01-13 15:34:35,819 INFO
...
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.04.16
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.05.08
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.05.22
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.06.17
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.03.26
2017-01-13 15:34:35,856 INFO      ---deleting index logstash-2016.05.20
2017-01-13 15:34:35,857 INFO      ---deleting index logstash-2016.04.23
2017-01-13 15:34:47,782 INFO      Action ID: 1, "delete_indices" completed.
2017-01-13 15:34:47,783 INFO      Job completed.

Finish.