Upon the death of Ibrahim Shah in 1685, his ten-year-old son, Mahmud Shah II, ascended the throne and the state affairs were left to Bendahara Tun Habib Abdul Majid. As he grew up, Mahmud Shah gained a reputation for his caprice, and Johor gradually descended into a state of chaos. This instability was exacerbated in 1697 by the death of Tun Habib.