API Reference / API Methods / Indexing / Replace all objects
Required API Key: Admin
Method signature
$index->replaceAllObjects(array objects);

$index->replaceAllObjects(array objects, [
  // All the following parameters are optional
  'safe' => bool,
]);

About this method

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

Clears all objects from your index and replaces them with a new set of objects.

Only your objects are replaced: settings, synonyms, and query rules aren’t modified. This method replaces all records in an index without any downtime.

Behind the scenes, replaceAllObjects uses a temporary index and:

  1. Copies your index’s settings, synonyms, and query rules to the temporary index
  2. Adds your objects to the temporary index
  3. Replaces your index with the temporary one.

If the API key has restricted index access, the API will return an error when attempting this operation. To fix this, make sure your API key has access to yourIndex and yourIndex_tmp_*.

Using this method can significantly increase your indexing operations and record count. It costs the number of new records + 2 operations (copySettings and moveIndex). For example, replacing all objects of an index with a new set of a million objects costs one million (and two) operations. Because your main and temporary index both have one million records, your application will temporarily host two million records. Make sure you don’t exceed your record limit, and be careful of the impact on your operations count.

Examples

Replace all objects

1
2
3
4
5
6
7
8
9
$client = Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

$objects = /* Fetch your objects */;

$index = $client->initIndex('your_index_name');
$index->replaceAllObjects($objects);

Replace all objects and wait for operations

1
2
3
4
5
6
7
8
9
10
11
$client = Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

$objects = /* Fetch your objects */;

$index = $client->initIndex('your_index_name');
$index->replaceAllObjects($objects, [
  'safe' => true,
]);

Parameters

objects
type: list
Required

A schemaless set of key-value pairs representing index attributes.

safe
type: boolean
default: false
Optional

Whether to wait for indexing operations.

requestOptions
type: key-value mapping
default: No request options
Optional

A mapping of requestOptions to send along with the query.

Response

No response

Did you find this page helpful?