Delete Records from Index
This guide assumes you have installed the latest version of klevu/php-sdk , either via composer or from source, and autoloading is functioning as expected.
Examples in this guide are for vanilla PHP and, as such, will use new to instantiate objects. If you are using a modern framework such as Magento or Symfony, you should utilise your object manager for dependency injection and preferencing of interfaces where possible.
Deleting Records from Index
Delete By ID
To delete records from the Klevu indexes, first locate your Klevu API and REST AUTH keys and create an AccountCredentials object (see Quickstart for steps).
Next, retrieve the IDs used to index any records you wish to delete.
Important: the ID must match the value used when adding the record initially.
As records of all types (KLEVU_PRODUCT, KLEVU_CATEGORY, etc) share an ID pool, you should implement a naming convention to differentiate records whose IDs would otherwise clash (for example, sending a product and a category, both with ID 1)
Create a new Indexing\Batch\DeleteService object and call the sendByIds method with the array of IDs collated previously, catching and handling any exceptions thrown during execution.
At this point, you will have an ApiResponseInterface which can be checked for success status, as well as any errors returned by the Klevu API.
<?php
declare(strict_types=1);
use Klevu\PhpSDK\Exception\Api\BadRequestException;
use Klevu\PhpSDK\Exception\Api\BadResponseException;
use Klevu\PhpSDK\Exception\ValidationException;
use Klevu\PhpSDK\Model\AccountCredentials;
use Klevu\PhpSDK\Service\Indexing\Batch\DeleteService as BatchDeleteService;
$accountCredentials = new AccountCredentials(
jsApiKey: "klevu-123456780",
restAuthKey: "ABCDE1234567890",
);
$batchDeleteService = new BatchDeleteService(
// Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
// as well as behaviour config, such as invalidRecordMode
);
$response = null;
try {
$response = $batchDeleteService->sendByIds(
accountCredentials: $accountCredentials,
recordIds: [
'1',
'2',
'category_1',
],
);
} catch (ValidationException $exception) {
// One or more passed arguments are invalid
} catch (BadRequestException $exception) {
// Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
// Klevu API responded in an unexpected manner
}
$deletedSuccessfully = $response?->isSuccess();
$messages = $response?->getMessages();Delete Objects
Alternatively, if you RecordInterface(s) already created, you can pass these to Batch\DeleteService::send . Records passed to send only require an id property.
<?php
declare(strict_types=1);
use Klevu\PhpSDK\Exception\Api\BadRequestException;
use Klevu\PhpSDK\Exception\Api\BadResponseException;
use Klevu\PhpSDK\Exception\ValidationException;
use Klevu\PhpSDK\Model\AccountCredentials;
use Klevu\PhpSDK\Model\Indexing\RecordFactory;
use Klevu\PhpSDK\Model\Indexing\RecordIterator;
use Klevu\PhpSDK\Service\Indexing\Batch\DeleteService as BatchDeleteService;
$accountCredentials = new AccountCredentials(
jsApiKey: "klevu-123456780",
restAuthKey: "ABCDE1234567890",
);
$recordFactory = new RecordFactory();
$records = new RecordIterator(
data: [
$recordFactory->create(['id' => '1']),
$recordFactory->create(['id' => '2']),
$recordFactory->create(['id' => 'category_1']),
],
);
$batchDeleteService = new BatchDeleteService(
// Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
// as well as behaviour config, such as invalidRecordMode or maxBatchSize
);
$response = null;
try {
$response = $batchDeleteService->send(
accountCredentials: $accountCredentials,
records: $records,
);
} catch (ValidationException $exception) {
// One or more passed arguments are invalid
} catch (BadRequestException $exception) {
// Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
// Klevu API responded in an unexpected manner
}
$deletedSuccessfully = $response?->isSuccess();
$messages = $response?->getMessages();