简体   繁体   中英

elasticsearch node.js delete all docs of an index

I'm trying to delete all documents from an index without delete the index itself via node.js API.

I'm trying with deleteByQuery but how can I specify all the docs inside the index?

You can simply use the REST API to delete all the docs from your index, for that you need not use the node.js API and you can directly hit below API. More info on delete all docs examples and different options

Delete all documents from an index

POST <your-index-name>/_delete_by_query
{
  "query": {
    "match_all": {} --> this matches all docs in index, hence deletes all of them.
  }
}

Curl format for as its a POST request, so it's difficult to use it with rest-client

curl -X POST "localhost:9200/<your-index-name>/_delete_by_query" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'

Thank to @opster.... I was searching for a pure node.js solution and finally I found it. I want to post it cause I've never seen this code snippet out there on the web.

_

On client using axios:

     axios({
        method: 'post',
        url: '/empty_index',
     }).then();

_

On server my express route

     app.post( '/empty_index', function( req, res, next ) {

        const client = new Client({
            node: 'http://localhost:'+process.env.ELASTICSEARCH_PORT
        });

        client.deleteByQuery({
            index: <your-index-name>,            
            body: {
                query: {
                    match_all: {}
                }
            }
        }, function (error, response) {
            console.log(response);
        });
        return res.status( 200 ).send();

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM