---
title: Add, Update, or Delete Indexing Attributes
slug: php-sdk/add-update-delete
docTags: 
createdAt: 2024-05-13T16:07:54.983Z
---

:::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.
:::

## Add or Update Attributes

:::hint{type="info"}
The following instructions are applicable to both Add and Update operations. When data is sent to Klevu's APIs, an attribute will be updated if it already exists and created otherwise.

Attributes are identified via the `attributeName` property.&#x20;
**Important:** this field is case-insensitive, so if you already have an attribute named, for example, `foo` and send a request with the name `FOO`, the original attribute will be updated, however the name will remain lowercase.
:::

To add or update custom attributes used by Klevu's JSON Indexing service, first locate your Klevu API and REST AUTH keys and create an `AccountCredentials` object (see Quickstart for steps).

Next, create an `AttributeInterface` object populated with the data to persist. Note, while you can create this using `new Attribute`, it is recommended to use the `AttributeFactory` class for ease of development and future compatibility.

Finally, instantiate an `AttributeService` object and call the `put` method with the Attribute object, catching and handling any exceptions thrown during execution.

:::hint{type="warning"}
If the attribute you are attempting to send is tagged as immutable ( `AttributeInterface::isImmutable()`), the SDK will prevent PUT operations during validation.
In this case, disabling the immutable flag and sending will cause the API to reject the request and the SDK to throw a `BadRequestException`
:::

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\Model\Indexing\Attribute;
use Klevu\PhpSDK\Model\Indexing\AttributeFactory;
use Klevu\PhpSDK\Model\Indexing\DataType;
use Klevu\PhpSDK\Service\Indexing\AttributesService;

$accountCredentials = new AccountCredentials(
    jsApiKey: "klevu-123456780",
    restAuthKey: "ABCDE1234567890",
);

$attributeFactory = new AttributeFactory();
$attribute = $attributeFactory->create(
    data: [
        Attribute::FIELD_ATTRIBUTE_NAME => 'example_attribute',
        Attribute::FIELD_DATATYPE => DataType::MULTIVALUE,
        Attribute::FIELD_LABEL => [
            'default' => 'Example Attribute',
        ],
        Attribute::FIELD_SEARCHABLE => true,
        Attribute::FIELD_FILTERABLE => false,
        Attribute::FIELD_RETURNABLE => true,
        Attribute::FIELD_IMMUTABLE => false,
    ],
);

$attributesService = new AttributesService(
    // Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);

$response = null;
try {
    $response = $attributesService->put(
        accountCredentials: $accountCredentials,
        attribute: $attribute,
    );
} catch (ValidationException $exception) {
    // One or more supplied arguments are missing or invalid
} catch (BadRequestException $exception) {
    // Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
    // Klevu API responded in an unexpected manner
}

$persistedSuccessfully = $response?->isSuccess();
$messages = $response?->getMessages();
```

## Delete Attributes

### Delete By Name

To delete custom attributes used by Klevu's JSON Indexing service, first locate your Klevu API and REST AUTH keys and create an `AccountCredentials` object (see Quickstart for steps).

Next, instantiate an `AttributeService` object and call the `deleteByName` method with the attributeName of the record you wish to remove, catching and handling any exceptions thrown during execution.

:::hint{type="warning"}
If the attribute is marked as immutable in Klevu's indexes, sending a DELETE request will cause the API to reject the request and the SDK to throw a `BadRequestException`
:::

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\AttributesService;

$accountCredentials = new AccountCredentials(
    jsApiKey: "klevu-123456780",
    restAuthKey: "ABCDE1234567890",
);

$attributesService = new AttributesService(
    // Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);

try {
    $response = $attributesService->deleteByName(
        accountCredentials: $accountCredentials,
        attributeName: 'example_attribute',
    );
} catch (ValidationException $exception) {
    // One or more supplied arguments are missing or 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 Object

&#x20;Alternatively, if you have an `AttributeInterface` already created, you can pass this to `AttributeService::delete` which acts as a wrapper around `deleteByName`.

Attributes passed to `delete` only require an `attributeName` property, which is internally extracted and passed to `deleteByName` . As such, attributes marked as immutable ( `AttributeInterface::isImmutable` will **not** be rejected by the SDK's validation.

```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\Attribute;
use Klevu\PhpSDK\Model\Indexing\AttributeFactory;
use Klevu\PhpSDK\Service\Indexing\AttributesService;

$accountCredentials = new AccountCredentials(
    jsApiKey: "klevu-123456780",
    restAuthKey: "ABCDE1234567890",
);

$attributeFactory = new AttributeFactory();
$attributesService = new AttributesService(
    // Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);

try {
    $response = $attributesService->delete(
        accountCredentials: $accountCredentials,
        attribute: $attributeFactory->create(
            data: [
                Attribute::FIELD_ATTRIBUTE_NAME => 'example_attribute',
            ],
        ),
    );
} catch (ValidationException $exception) {
    // One or more supplied arguments are missing or 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\\[AttributeInterface](https://github.com/klevu/php-sdk/blob/master/src/Api/Model/Indexing/AttributeInterface.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\\[Attribute](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/Attribute.php)
- Model\Indexing\\[AttributeFactory](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/AttributeFactory.php)
- Model\Indexing\\[DataType](https://github.com/klevu/php-sdk/blob/master/src/Model/Indexing/DataType.php)
- Service\Indexing\\[AttributesService](https://github.com/klevu/php-sdk/blob/master/src/Service/Indexing/AttributesService.php)

