---
title: Delete Records from Index
slug: php-sdk/delete-records-from-index
docTags: 
createdAt: 2024-05-13T16:20:36.746Z
---

:::hint{type="info"}
*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.*
:::

:::hint{type="info"}
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.

:::hint{type="warning"}
**Important**: the ID must match the value used when adding the record initially.&#x20;

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
<?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
<?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();
```

***

## Source Code Reference

- Api\Model\\[Indexing\RecordInterface](https://github.com/klevu/php-sdk/blob/master/src/Api/Model/Indexing/RecordInterface.php)
- Exception\Api\\[BadRequestException](https://github.com/klevu/php-sdk/blob/master/src/Exception/Api/BadRequestException.php)
- Exception\Api\\[BadResponseException](https://github.com/klevu/php-sdk/blob/master/src/Exception/Api/BadResponseException.php)
- Exception\\[ValidationException](https://github.com/klevu/php-sdk/blob/master/src/Exception/ValidationException.php)
- Model\\[AccountCredentials](https://github.com/klevu/php-sdk/blob/master/src/Model/AccountCredentials.php)
- Model\\[Indexing\Record](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/Record.php)
- Model\\[Indexing\RecordFactory](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/RecordFactory.php)
- Model\\[Indexing\RecordIterator](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/RecordIterator.php)
- Service\\[Indexing\Batch\DeleteService](https://github.com/klevu/php-sdk/blob/master/src/Service/Indexing/Batch/DeleteService.php)

