quality:corruption, information:filtering, structural:summarization, structural:scholar_edition, structural:expansion
Yogyakarta secedes and European invasions (1745–1830)

A civil war in the Mataram Sultanate broke out between Pakubuwono II (1745–1749), the last ruler of Kartasura, and his younger brother and heir apparent to the throne, Prince Mangkubumi (later known as Hamengkubuwono I, the first Sultan of Yogyakarta, and the founder of the current ruling royal house). Pakubuwono II had agreed to cooperate with the Dutch East India Company, and ceded some Mataram territory to the Dutch. Prince Mangkubumi, stood against the agreement, citing concerns that the people would become slaves under Dutch rule. During the war, Prince Mangkubumi defeated Pakubuwono II's forces and declared sovereignty in the Sultanate of Yogyakarta, occupying the southern parts of the former Mataram Sultanate.

With Pakubowono II dead from illness, the Yogyakarta Sultanate was established as a result of the Treaty of Giyanti (Perjanjian Gianti), signed and ratified on 13 February 1755 among Prince Mangkubumi, the Dutch East India Company, and his nephew Pakubuwono III and his allies. Ascending to the newly created Yogyakarta throne with the name Sultan Hamengkubuwono I, Mangkubumi thus established the royal House of Hamengkubuwono, still the ruling house of Yogyakarta today. Sultan Hamengkubuwono I and his family officially moved into the Palace of Yogyakarta, still the seat of the reigning sultan, on 7 October 1756. These events consequently marked the end of the Mataram Sultanate, resulting in the births of the rival Yogyakarta Sultanate and the Surakarta Sunanate.

During the brief period of British rule over Java in 1811, rumours of plans by the Yogyakarta court to launch an attack against the British led to uneasiness among the Britons stationed in Java. On 20 June 1812, Sir Stamford Raffles led a 1,200-strong British force to capture the Yogyakarta kraton. The Yogyakarta forces, surprised by the attack, were easily defeated; the kraton fell in one day, and was subsequently sacked and burnt.

The attack on the kraton was the first of its kind in Indonesian history, leaving the Yogyakarta court humiliated. The sultanate found itself involved in conflict again during the Java War.