AlgoliaSearch\Index::deleteByQuery PHP Method

deleteByQuery() public method

Delete all objects matching a query.
public deleteByQuery ( string $query, array $args = [], boolean $waitLastCall = true ) : integer
$query string the query string
$args array the optional query parameters
$waitLastCall boolean /!\ Be safe with "waitLastCall" In really rare cases you can have the number of hits smaller than the hitsPerPage param if you trigger the timeout of the search, in that case you won't remove all the records
return integer the number of delete operations
    public function deleteByQuery($query, $args = array(), $waitLastCall = true)
    {
        $args['attributesToRetrieve'] = 'objectID';
        $args['hitsPerPage'] = 1000;
        $args['distinct'] = false;
        $deletedCount = 0;
        $results = $this->search($query, $args);
        while ($results['nbHits'] != 0) {
            $objectIDs = array();
            foreach ($results['hits'] as $elt) {
                array_push($objectIDs, $elt['objectID']);
            }
            $res = $this->deleteObjects($objectIDs);
            $deletedCount += count($objectIDs);
            if ($results['nbHits'] < $args['hitsPerPage'] && false === $waitLastCall) {
                break;
            }
            $this->waitTask($res['taskID']);
            $results = $this->search($query, $args);
        }
        return $deletedCount;
    }