Ionic framework - Remove the last view from history stack

I try to do the following:

I have 3 pages in this order: mainPage->secondPage->thirdPage

I want the back button at thirdPage to go back to the page before secondPage, hence: mainPage.

any thoughts how to implement this?

Just use

$location.replace('thirdPage');

Instead of

$location.path('thirdPage');

It will swap secondPage to thirdPage in the history, and so the back will get you to mainPage. Doc : $location