# Is adding and removing an index with a single call to the aliases api atomic?

**URL:** <https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615>\
**Category:** Elasticsearch\
**Created:** [May 9, 2012, 2:10pm UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615 "2012-05-09T14:10:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederick_cheung/32/1874_2.png) [@Frederick\_Cheung](https://discuss.elastic.co/u/Frederick_Cheung)\
**Post date:** [May 9, 2012, 2:10pm UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615/1 "2012-05-09T14:10:39Z")

</div>

Hi,

When we need to rebuild an index from scratch we handle the transition  
using aliases, for example we'd start off with garments-2012-04-01  
having the alias garments, then we'd build garments-2012-05-01 and  
when we're done we change the aliases around so that garments is now  
an alias for garments-2012-05-01. The app always queries the index  
using the garments aliases and so it gets a smooth transition from 1  
index to another.

We use to do this by doing a delete on garments (which thus deleting  
garments-2012-04-01) and then adding the garments alias to  
garments-2012-05-01. While this obviously created a window in which  
queries would fail, what I had forgotten about was that since some  
apis automatically create the index, there was a race condition where  
a garments index would be created automatically and my adding the new  
alias would fail, so the app is left using the empty auto created  
index.

I was considering shuffling the indexes around by removing and adding  
the index in one call to the aliases api, something like

curl -XPOST '[http://localhost:9200/\_aliases](http://localhost:9200/_aliases)' -d '  
{  
"actions" : [  
{ "remove" : { "index" : "garments-2012-04-01", "alias" :  
"garments" } },  
{ "add" : { "index" : "garments-2012-05-01", "alias" :  
"garments" } }  
]  
}'

Does this avoid the pitfall I encountered previously or am I better  
off just turning off action.auto\_create\_index ?

Thanks for any advice,

Fred

---

<div class="post-metadata">

**Author:** ![Andrew\_at\_DataFeedFi](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@Andrew\_at\_DataFeedFi](https://discuss.elastic.co/u/Andrew_at_DataFeedFi)\
**Post date:** [May 9, 2012, 2:36pm UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615/2 "2012-05-09T14:36:37Z")

</div>

Have you tried adding BOTH indices into a single Alias, then you can  
delete one index while the other still exists.  
Would that solve your problem?

On May 9, 9:10 am, Frederick Cheung [frederick.che...@gmail.com](mailto:frederick.che...@gmail.com)  
wrote:

> Hi,
> 
> When we need to rebuild an index from scratch we handle the transition  
> using aliases, for example we'd start off with garments-2012-04-01  
> having the alias garments, then we'd build garments-2012-05-01 and  
> when we're done we change the aliases around so that garments is now  
> an alias for garments-2012-05-01. The app always queries the index  
> using the garments aliases and so it gets a smooth transition from 1  
> index to another.
> 
> We use to do this by doing a delete on garments (which thus deleting  
> garments-2012-04-01) and then adding the garments alias to  
> garments-2012-05-01. While this obviously created a window in which  
> queries would fail, what I had forgotten about was that since some  
> apis automatically create the index, there was a race condition where  
> a garments index would be created automatically and my adding the new  
> alias would fail, so the app is left using the empty auto created  
> index.
> 
> I was considering shuffling the indexes around by removing and adding  
> the index in one call to the aliases api, something like
> 
> curl -XPOST '[http://localhost:9200/\_aliases'-d](http://localhost:9200/_aliases'-d) '  
> {  
> "actions" : [  
> { "remove" : { "index" : "garments-2012-04-01", "alias" :  
> "garments" } },  
> { "add" : { "index" : "garments-2012-05-01", "alias" :  
> "garments" } }  
> ]
> 
> }'
> 
> Does this avoid the pitfall I encountered previously or am I better  
> off just turning off action.auto\_create\_index ?
> 
> Thanks for any advice,
> 
> Fred

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 9, 2012, 8:01pm UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615/3 "2012-05-09T20:01:16Z")

</div>

Yes, thats the idea of the aliases API, the "operations" that you specify  
there are atomic (all or nothing). Note, teh remove simply states that the  
alias will be removed, not that the index will be removed/deleted.

Also, you can set action.auto\_create\_index to false in the config file  
which will disable automatically creating an index if it does not exists  
with the index API.

On Wed, May 9, 2012 at 5:10 PM, Frederick Cheung \<[frederick.cheung@gmail.com](mailto:frederick.cheung@gmail.com)

> wrote:

> Hi,
> 
> When we need to rebuild an index from scratch we handle the transition  
> using aliases, for example we'd start off with garments-2012-04-01  
> having the alias garments, then we'd build garments-2012-05-01 and  
> when we're done we change the aliases around so that garments is now  
> an alias for garments-2012-05-01. The app always queries the index  
> using the garments aliases and so it gets a smooth transition from 1  
> index to another.
> 
> We use to do this by doing a delete on garments (which thus deleting  
> garments-2012-04-01) and then adding the garments alias to  
> garments-2012-05-01. While this obviously created a window in which  
> queries would fail, what I had forgotten about was that since some  
> apis automatically create the index, there was a race condition where  
> a garments index would be created automatically and my adding the new  
> alias would fail, so the app is left using the empty auto created  
> index.
> 
> I was considering shuffling the indexes around by removing and adding  
> the index in one call to the aliases api, something like
> 
> curl -XPOST '[http://localhost:9200/\_aliases](http://localhost:9200/_aliases)' -d '  
> {  
> "actions" : [  
> { "remove" : { "index" : "garments-2012-04-01", "alias" :  
> "garments" } },  
> { "add" : { "index" : "garments-2012-05-01", "alias" :  
> "garments" } }  
> ]  
> }'
> 
> Does this avoid the pitfall I encountered previously or am I better  
> off just turning off action.auto\_create\_index ?
> 
> Thanks for any advice,
> 
> Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederick_cheung/32/1874_2.png) [@Frederick\_Cheung](https://discuss.elastic.co/u/Frederick_Cheung)\
**Post date:** [May 10, 2012, 3:15pm UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615/4 "2012-05-10T15:15:45Z")

</div>

On May 9, 9:01 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Yes, thats the idea of the aliases API, the "operations" that you specify  
> there are atomic (all or nothing). Note, teh remove simply states that the  
> alias will be removed, not that the index will be removed/deleted.

Excellent, thanks for the info.

Fred

> Also, you can set action.auto\_create\_index to false in the config file  
> which will disable automatically creating an index if it does not exists  
> with the index API.

> On Wed, May 9, 2012 at 5:10 PM, Frederick Cheung \<[frederick.che...@gmail.com](mailto:frederick.che...@gmail.com)
> 
> > wrote:  
> > Hi,
> 
> > When we need to rebuild an index from scratch we handle the transition  
> > using aliases, for example we'd start off with garments-2012-04-01  
> > having the alias garments, then we'd build garments-2012-05-01 and  
> > when we're done we change the aliases around so that garments is now  
> > an alias for garments-2012-05-01. The app always queries the index  
> > using the garments aliases and so it gets a smooth transition from 1  
> > index to another.
> 
> > We use to do this by doing a delete on garments (which thus deleting  
> > garments-2012-04-01) and then adding the garments alias to  
> > garments-2012-05-01. While this obviously created a window in which  
> > queries would fail, what I had forgotten about was that since some  
> > apis automatically create the index, there was a race condition where  
> > a garments index would be created automatically and my adding the new  
> > alias would fail, so the app is left using the empty auto created  
> > index.
> 
> > I was considering shuffling the indexes around by removing and adding  
> > the index in one call to the aliases api, something like
> 
> > curl -XPOST '[http://localhost:9200/\_aliases'-d](http://localhost:9200/_aliases'-d) '  
> > {  
> > "actions" : [  
> > { "remove" : { "index" : "garments-2012-04-01", "alias" :  
> > "garments" } },  
> > { "add" : { "index" : "garments-2012-05-01", "alias" :  
> > "garments" } }  
> > ]  
> > }'
> 
> > Does this avoid the pitfall I encountered previously or am I better  
> > off just turning off action.auto\_create\_index ?
> 
> > Thanks for any advice,
> 
> > Fred

---

<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:** [July 6, 2017, 3:29am UTC](https://discuss.elastic.co/t/is-adding-and-removing-an-index-with-a-single-call-to-the-aliases-api-atomic/7615/5 "2017-07-06T03:29:26Z")

</div>


