> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Replace all rules

> Push a new set of rules and erase all previous ones.

export const Legacy = ({title, href}) => {
  return <Note>

    This page documents an earlier version of the API client.
    For the latest version, see <a href={href}>{title}</a>.

    </Note>;
};

<Legacy title="Create or update rules" href="/doc/libraries/sdk/methods/search/save-rules" />

**Required ACL:** `editSettings`

This method, like `replaceAllObjects`, guarantees zero downtime.

All existing rules are deleted and replaced with the new ones, in a single, atomic operation.

## Examples

<CodeGroup>
  ```cs C# theme={"system"}
  SearchClient client = new SearchClient("YourApplicationID", "YourWriteAPIKey");
  SearchIndex index = client.InitIndex("INDEX_NAME");

  List<Rule> rules = /* Fetch your rules */

  index.ReplaceAllRules(rules);

  // Or if you want to also replace synonyms on replicas
  index.ReplaceAllRules(synonyms, forwardToReplicas: true);

  // Asynchronous
  await index.ReplaceAllRulesAsync(synonyms, forwardToReplicas: true);
  ```

  ```go Go theme={"system"}
  rules := []search.Rule{ /* Fetch your rules */ }

  res, err := index.ReplaceAllRules(rules)
  ```

  ```java Java theme={"system"}
  SearchClient client = 
  DefaultSearchClient.create("YourApplicationID", "YourWriteAPIKey");

  SearchIndex index = client.initIndex("INDEX_NAME");

  List<Rule> rules = /* Fetch your rules */

  index.replaceAllRules(rules);

  // Or if you want to also replace rules on replicas
  index.replaceAllRules(rules, true);

  // Async
  index.replaceAllRules(synonyms);
  index.replaceAllRules(synonyms, true);
  ```

  ```js JavaScript theme={"system"}
  const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');

  const index = client.initIndex('INDEX_NAME');
  const rules = [/* Fetch your rules */];

  index.replaceAllRules(rules).then(() => {
    // done
  });

  // Or if you want to also replace rules on replicas
  index.replaceAllRules(rules, { forwardToReplicas: true }).then(() => {
    // done
  });
  ```

  ```kotlin Kotlin theme={"system"}
  // Fetch your rules
  val rules = listOf<Rule>()

  index.replaceAllRules(rules, forwardToReplicas = true)
  ```

  ```php PHP theme={"system"}
  $client = Algolia\AlgoliaSearch\SearchClient::create(
    'YourApplicationID',
    'YourWriteAPIKey'
  );

  $rules = /* Fetch your rules */;

  $index = $client->initIndex('INDEX_NAME');
  $index->replaceAllRules($rules);

  // Or if you want to also replace rules on replicas
  $index->replaceAllRules($rules, ['forwardToReplicas' => true]);
  ```

  ```python Python theme={"system"}
  client = algoliasearch.Client("YourApplicationID", 'YourWriteAPIKey')

  rules = [] # Fetch your rules

  index = client.init_index('INDEX_NAME')
  index.replace_all_rules(rules)

  # Or if you want to also replace rules on replicas
  index.replace_all_rules(rules, { 'forwardToReplicas': True })
  ```

  ```ruby Ruby theme={"system"}
  client = Algolia::Search::Client.create('YourApplicationID', 'YourWriteAPIKey')

  rules = [] # Fetch your rules

  index = client.init_index('INDEX_NAME')
  index.replace_all_rules(rules)

  # Or if you want to also replace rules on replicas
  index.replace_all_rules(rules, {'forwardToReplicas': true})
  ```

  ```swift Swift theme={"system"}
  let client = SearchClient(appID: "YourApplicationID", apiKey: "YourWriteAPIKey")
  let index = client.index(withName: "INDEX_NAME")

  let rules: [Rule] = [/* Fetch your rules */]

  index.replaceAllRules(with: rules) { result in
    if case .success(let response) = result {
      print("Response: \(response)")
    }
  }

  // Or if you want to also replace synonyms on replicas
  index.replaceAllRules(with: rules,
                        forwardToReplicas: true) { result in
    if case .success(let response) = result {
      print("Response: \(response)")
    }
  }
  ```
</CodeGroup>

## Parameters

<ParamField body="rules" type="list" required>
  See the [`rule`](/doc/libraries/sdk/v1/methods/save-rule#param-rule) parameter of the `saveRule` method.
</ParamField>

<ParamField body="forwardToReplicas" type="boolean" default={false}>
  Whether to also replace all rules on replicas.
</ParamField>

<ParamField body="requestOptions" type="object">
  A mapping of request options to send along with the request.
</ParamField>
