<commit_before>[[indices-shrink-index]]
== Shrink Index

The shrink index API allows you to shrink an existing index into a new index
with fewer primary shards. The requested number of primary shards in the target index
must be a factor of the number of shards in the source index. For example an index with
`8` primary shards can be shrunk into `4`, `2` or `1` primary shards or an index
with `15` primary shards can be shrunk into `5`, `3` or `1`. If the number
of shards in the index is a prime number it can only be shrunk into a single
primary shard. Before shrinking, a (primary or replica) copy of every shard
in the index must be present on the same node.

Shrinking works as follows:

* First, it creates a new target index with the same definition as the source
  index, but with a smaller number of primary shards.

* Then it hard-links segments from the source index into the target index. (If
  the file system doesn't support hard-linking, then all segments are copied
  into the new index, which is a much more time consuming process.)

* Finally, it recovers the target index as though it were a closed index which
  had just been re-opened.

[float]
=== Preparing an index for shrinking

In order to shrink an index, the index must be marked as read-only, and a
(primary or replica) copy of every shard in the index must be relocated to the
same node and have <<cluster-health,health>> `green`.
<commit_msg>shrink may full copy when using multi data paths (#42913) (#42965)<commit_after>[[indices-shrink-index]]
== Shrink Index

The shrink index API allows you to shrink an existing index into a new index
with fewer primary shards. The requested number of primary shards in the target index
must be a factor of the number of shards in the source index. For example an index with
`8` primary shards can be shrunk into `4`, `2` or `1` primary shards or an index
with `15` primary shards can be shrunk into `5`, `3` or `1`. If the number
of shards in the index is a prime number it can only be shrunk into a single
primary shard. Before shrinking, a (primary or replica) copy of every shard
in the index must be present on the same node.

Shrinking works as follows:

* First, it creates a new target index with the same definition as the source
  index, but with a smaller number of primary shards.

* Then it hard-links segments from the source index into the target index. (If
  the file system doesn't support hard-linking, then all segments are copied
  into the new index, which is a much more time consuming process. Also if using 
  multiple data paths, shards on different data paths require a full copy of 
  segment files if they are not on the same disk since hardlinks don’t work across
  disks)

* Finally, it recovers the target index as though it were a closed index which
  had just been re-opened.

[float]
=== Preparing an index for shrinking

In order to shrink an index, the index must be marked as read-only, and a
(primary or replica) copy of every shard in the index must be relocated to the
same node and have <<cluster-health,health>> `green`.

{'fraction_non_alphanumeric': 0.05121555915721232, 'fraction_numerical': 0.00907617504051864, 'mean_word_length': 4.050736497545008, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2969470', 'n_tokens_mistral': 856, 'n_tokens_neox': 809, 'n_words': 528}