 Mahmud later died in 1030, and a civil war shortly ensued between his two sons, the youngest one being the heir Mohammad Ghaznavi (who shortly had Hasanak changed with Abu Sahl Hamduwi as "vizier"), and the oldest one being Mas'ud I. Hasanak, along with Ali ibn Il-Arslan, supported Mohammad, and both expected that they would hold absolute power over the Ghaznavid state, while Mohammad would stay as a figurehead.However, Ali, including other Ghaznavid statesman, began to become more distant from Hasanak, and changed their adherence to Mas'ud I.

Hasanak, however, continued to support Mohammad, but Mas'ud I shortly marched towards the Ghaznavid capital of Ghazni, where he successfully defeated Mohammad and imprisoned the latter.He then had Hasanak imprisoned in Balkh, while restoring Ahmad Maymandi to the vizier office.