Skip to content

Instantly share code, notes, and snippets.

@shentonfreude
Created August 15, 2017 21:02
Show Gist options
  • Save shentonfreude/9cf7f1fdec2f84b9e035f2fbbd3c9f02 to your computer and use it in GitHub Desktop.
Save shentonfreude/9cf7f1fdec2f84b9e035f2fbbd3c9f02 to your computer and use it in GitHub Desktop.
Delete all docs from AWS Elasticsearch Service -- useful for development
"""Get all doc ids and delete them to provide a clean index for demos, etc."""
import os
import sys
from aws_requests_auth.aws_auth import AWSRequestsAuth
from elasticsearch import Elasticsearch, RequestsHttpConnection
try:
es_index = sys.argv[1]
es_doctype = sys.argv[2]
es_host = sys.argv[3]
es_region = 'us-east-1'
print(len(sys.argv))
if len(sys.argv) > 4:
es_region = sys.argv[4]
except IndexError as e:
print('Usage: {} index, doctype, hostname, [region]'.format(sys.argv[0]))
print(' AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY must be in environ')
exit(-1)
es = Elasticsearch(host=es_host, port=80)
auth = AWSRequestsAuth(
aws_access_key=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
aws_host=es_host,
aws_region=es_region,
aws_service='es',
)
es = Elasticsearch(host=es_host,
port=80,
connection_class=RequestsHttpConnection,
http_auth=auth)
res = es.search(index='ocreva', body={'query': {'match_all': {}}})
ids = [h['_id'] for h in res['hits']['hits']]
print('Nuking ids: {}'.format(ids))
for id in ids:
es.delete(index='ocreva', doc_type='pdf', id=id)
res = es.indices.delete(index=es_index, ignore=[400, 404])
print('Deleted index: {}'.format(res)) working in the GC environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment