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

# Save rules

> Create or update a list of rules.

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`

Each rule is created or updated, depending on whether a rule with the same `objectID` already exists.

## Examples

<CodeGroup>
  ```cs C# theme={"system"}
  List<Rule> rules;
  index.SaveRules(rules, forwardToReplicas: true, clearExistingRules: false);

  // Asynchronous
  await index.SaveRulesAsync(rules, forwardToReplicas: true, clearExistingRules: false);
  ```

  ```go Go theme={"system"}
  rules := []search.Rule{rule1, rule2}

  opts := []interface{}{
  	opt.ForwardToReplicas(false),
  	opt.ClearExistingRules(false),
  }

  res, err := index.SaveRules(rules, opts...)
  ```

  ```java Java theme={"system"}
  boolean forwardToReplicas = false;
  boolean clearExistingRules = false;

  index.saveRules(
    Arrays.asList(rule1, rule2),
    forwardToReplicas,
    clearExistingRules
  );

  index.saveRulesAsync(
    Arrays.asList(rule1, rule2),
    forwardToReplicas,
    clearExistingRules
  );
  ```

  ```js JavaScript theme={"system"}
  const rules = [
    {
      objectID: "rule-1",
      conditions: [
        {
          pattern: "smartphone",
          anchoring: "contains",
        },
      ],
      consequence: {
        params: {
          filters: "category = 1",
        },
      },
    },
    {
      objectID: "rule-2",
      conditions: [
        {
          pattern: "tv",
          anchoring: "contains",
        },
      ],
      consequence: {
        params: {
          filters: "category = 2",
        },
      },
    },
  ];

  // Save the Rules.
  index.saveRules(rules).then(() => {
    // done
  });

  // Replace all the rules in the index by the provided ones.
  index.saveRules(rules, { clearExistingRules: true }).then(() => {
    // done
  });

  // Save the Rules, and forward them to all replicas of the index.
  index.saveRules(rules, { forwardToReplicas: true }).then(() => {
    // done
  });
  ```

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

  index.saveRules(rules, forwardToReplicas = true, clearExistingRules = true)
  ```

  ```php PHP theme={"system"}
  $rules = [$rule1, $rule2];

  $index->saveRules($rules);
  ```

  ```python Python theme={"system"}
  rules = [rule1, rule2]

  index.save_rules(rules)
  ```

  ```ruby Ruby theme={"system"}
  rules = [rule1, rule2]

  # Save the Rules.
  index.batch_rules(rules)
  # Save the Rules and wait the end of indexing.
  index.batch_rules!(rules)
  # Replace all the rules in the index by the provided ones.
  index.batch_rules(rules, {forwardToReplicas: true, clearExistingRules: true})
  ```

  ```scala Scala theme={"system"}
  client.execute {
    save rules Seq(rule1, rule2) inIndex "index_name"
  }
  ```

  ```swift Swift theme={"system"}
  let rules: [Rule] = [/* Your Query rules */]

  index.saveRules(rules,
                  forwardToReplicas: true,
                  clearExistingRules: true) { result in
    if case .success(let response) = result {
      print("Response: \(response)")
    }
  }
  ```
</CodeGroup>

## Parameters

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

<ParamField body="clearExistingRules" type="boolean" default={false}>
  Whether to delete all existing rules before adding new ones.
</ParamField>

<ParamField body="forwardToReplicas" type="boolean" default={false}>
  Whether to forward the changes to the replica indices.
</ParamField>

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

## Response

<ResponseField name="taskID" type="integer">
  The task ID used with the [`waitTask`](/doc/libraries/sdk/v1/methods/wait-task) method.
</ResponseField>

<ResponseField name="updatedAt" type="string">
  Date at which the job has been created.
</ResponseField>

### Response as JSON

This section shows the JSON response returned by the API.
Each API client wraps this response in language-specific objects, so the structure may vary.
To view the response, use the `getLogs` method.
Don't rely on the order of properties—JSON objects don't preserve key order.

```jsonc JSON icon=braces theme={"system"}
{
  "updatedAt":"2013-01-18T15:33:13.556Z",
  "taskID": 678
}
```
