# How do I increase or reduce the shard count of an existing index?

**URL:** <https://discuss.elastic.co/t/how-do-i-increase-or-reduce-the-shard-count-of-an-existing-index/242704>\
**Category:** Elastic Tips and Common Fixes\
**Tags:** elasticsearch\
**Created:** [July 27, 2020, 7:37am UTC](https://discuss.elastic.co/t/how-do-i-increase-or-reduce-the-shard-count-of-an-existing-index/242704 "2020-07-27T07:37:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 27, 2020, 7:37am UTC](https://discuss.elastic.co/t/how-do-i-increase-or-reduce-the-shard-count-of-an-existing-index/242704/1 "2020-07-27T07:37:17Z")

</div>

Traditionally, once you created an index with a given number of **primary** shards, it was set until you reindexed your data. That meant that if you hit the limit of documents in a shard, you might have been caught in a bit of trouble.

However, as of Elasticsearch 5.0, you can [`_shrink`](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html), and as of Elasticsearch 6.1, you can [`_split`](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/indices-split-index.html) an index. Thereby increasing and decreasing the number of shards.

# Reducing your shard count - aka `_shrink`

Shrinking is used in [ILM](https://www.elastic.co/guide/en/elasticsearch/reference/current/ilm-start.html), and more generally, for reducing [excessing shard counts](https://www.elastic.co/guide/en/elasticsearch/reference/current/avoid-oversharding.html).

[Before you can shrink an index](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#shrink-index-api-prereqs):

- The index must be read-only.
- All primary shards for the index must reside on the same node.
- The index must have a `green`[health status](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html).

The other important thing to take into account is the number of primary shards in the original index, as you can only shrink to [a factor of that count](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shrink-index.html#shrink-index-api-desc). That means for 6 primary shards, you can shrink to 3 or 2 or 1. For 9 it'd be 3 or 1.

Here's an example, adapted from the docs. Let's assume we created an index with 4 primary shards, and one set of replicas;

```auto
PUT my_source_index
{
  "settings": {
    "number_of_shards": 4,
    "number_of_replicas": 1
  }
}

```

Now we want to shrink the `my_source_index` index, which starts with the 3 prerequisite steps above. You will need to replace `shrink_node_name` with an actual node name, which you can get with `_cat/nodes?v`;

```auto
PUT /my_source_index/_settings?pretty
{
  "settings": {
    "index.number_of_replicas": 0,
    "index.routing.allocation.require._name": "instance-0000000000",
    "index.blocks.write": true   
  }
}

```

Then we tell Elasticsearch we want to run the shrink process and output that into a new index called `my_target_index`, with 1 primary shard;

```auto
POST /my_source_index/_shrink/my_target_index
{
  "settings": {
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  }
}

```

We can check the progress with;

```auto
GET _cat/recovery?v

```

Once it's all done, you probably want to remove the original index, and then [create an alias](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-add-alias.html) so that anyone still using the old index name doesn't find their data missing;

```auto
DELETE my_source_index
PUT /my_target_index/_alias/my_source_index

```

# Increasing your shard count - aka `_split`

This is really similar to what we did with the shrink, just in reverse. **NOTE** - if you are using [ILM](https://www.elastic.co/guide/en/elasticsearch/reference/current/ilm-start.html), you probably won't want to use this split functionality. Let the ILM policy rollover to a new index instead.

[Before you can split an index:](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html)

- The index must be read-only.
- The [cluster health](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html) status must be green.

Like a shrink, we need to expand the shard count by factors. This is influenced by `number_of_routing_shards`, which is an index setting that can be applied during index creation, or dynamically _if the index has been closed_ (you'd then reopen it after the change). [The docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-split-index.html#split-index-api-desc) dive into this aspect in more detail.

Using an index similar to one we created above, let's set the index to be read-only;

```auto
PUT /my_source_index/_settings
{
  "settings": {
    "index.blocks.write": true 
  }
}

```

And then run the split process itself;

```auto
POST /my_source_index/_split/my_target_index
{
  "settings": {
    "index.number_of_shards": 2
  }
}

```

Check the progress with;

```auto
GET _cat/recovery?v

```

Again, once it's all done, you probably want to remove the original index, and then [create an alias](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-add-alias.html) so that anyone still using the old index name doesn't find their data missing;

```auto
DELETE my_source_index
PUT /my_target_index/_alias/my_source_index

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 8:07am UTC](https://discuss.elastic.co/t/how-do-i-increase-or-reduce-the-shard-count-of-an-existing-index/242704/2 "2022-11-04T08:07:49Z")

</div>


