We don’t recommend using deleteBy as this intensive task can take time to complete particularly on larger data sets. For that reason we suggest using a combination of browse and delete to achieve the same functionality.
You can achieve the same general functionality as deleteBy by collecting the objectIDs of the records you return with the browse query, sending the filter you’d use to deleteBy with the parameters. Note that this requires you setting the relevant filter as an attributesForFaceting.
Below is a javascript example of this in full. The example uses the browse method to identify objectIDs of computers with the brand samsung. It them passes the objectIDs to the delete method to remove the items from the index
// A global variable to store id's
let matchingRecordIds = [];
// Browse documentation can be found here in full
// https://www.algolia.com/doc/api-reference/api-methods/browse/
// Supported parameters can be found here
// https://www.algolia.com/doc/api-reference/api-methods/browse/#method-param-browseparameters
index.browseObjects({
batch: (hits) => {
// Use this function to parse the response.
// We used browseParameters below to restrict to only
// get objectIds as this should be enough to delete by.
const hitIds = hits.map(hit => hit.objectID)
// The response is then added to a global array
matchingRecordIds = matchingRecordIds.concat(hitIds)
},
// You can use any text based query
query: "computers",
// Restrict to only get the id of records to delete
attributesToRetrieve: ["objectID"],
// Apply a filter
filters: "brand:samsung"
}).then(() => {
// Once the browse has completed delete the objects from the index
// https://www.algolia.com/doc/api-reference/api-methods/delete-objects/
index.deleteObjects(matchingRecordIds)
.wait().then(() => console.log(`${matchingRecordIds.length} items removed`));
})
.catch((err) => console.log(err));