Examples / How To
Data Indexing

Delete Records from Index

6min
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(); source code reference api\model\ indexing\recordinterface exception\api\ badrequestexception exception\api\ badresponseexception exception\ validationexception model\ accountcredentials model\ indexing\record model\ indexing\recordfactory model\ indexing\recorditerator service\ indexing\batch\deleteservice