Algolia: Delete multiple records from dashboard
Asked Answered
D

2

8

How can I delete multiple records at once? Is it possible to select all, say "products" post_type, and delete it or it has to be one by one? (I'm not trying to clear all the records)

Daly answered 18/8, 2016 at 22:20 Comment(0)
A
4

Algolia's dashboard is not designed to be a complete graphical interface on top of the API, it's mostly here for convenience, understanding and testing purposes, not complete management of the data.

As soon as you start being limited by the dashboard, you should probably write a small script to achieve what you're trying to do.

Fortunately, it's been designed to be as easy as possible.
With PHP, here's how it would look like:


First, let's create a small folder to hold the script.

mkdir /tmp/clear-algolia && cd /tmp/clear-algolia

If you don't have composer yet, you can simply install it in the current folder by launching the commands described here.
If you've just installed it and want to just use it just for this session:

alias composer=php composer.phar

Then install Algolia using composer:

composer require algolia/algoliasearch-client-php

Write a small script along those lines:

<?php
// removeSpecific.php

require __DIR__ . '/vendor/autoload.php';

$client = new \AlgoliaSearch\Client("YOUR_APP_ID", "YOUR_ADMIN_API_KEY");
$index = $client->initIndex('YOUR_INDEX');
$index->deleteByQuery('', [ 'filters' => 'post_type:products' ]);

?>

Then run it:

php removeSpecific.php

And you're good to go! Next time you want to do an operation on your index, you'll only have to change the last line of the script to achieve what you want.

Author answered 19/8, 2016 at 9:27 Comment(0)
C
0

You can use the REST API.

It can be easier or faster to do it with PostMan.

Here you can check a simple request: https://www.algolia.com/doc/rest-api/search/#delete-by

To first check what you are deleting, you can use:

curl --location --request POST 'https://[AplicationID]-
dsn.algolia.net/1/indexes/[IndexName]/query' \
--header 'X-Algolia-Application-Id: XXXXXXXXXXXX' \
--header 'X-Algolia-API-Key: XXXXXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data-raw '{
    "params":"numericFilters=id<=9000"
}'

And to delete the records you can use:

curl --location --request POST 
'https://[AplicationID].algolia.net/1/indexes/[IndexName]/deleteByQuery' \
--header 'X-Algolia-Application-Id: XXXXXXXXXXXX' \
--header 'X-Algolia-API-Key: XXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data-raw '{
    "params":"numericFilters=id<=8000"
}'

The "params" should receive a Search Parameter, you can find a list here: https://www.algolia.com/doc/api-reference/search-api-parameters/

Chrestomathy answered 15/12, 2022 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.