In spring 618, Emperor Yang was killed at Jiangdu (, in modern
Yangzhou,
Jiangsu) in a coup led by the general
Yuwen Huaji. When the news reached Chang'an, Li Yuan had Yang You yield the throne to him, becoming Emperor Gaozu of the
Tang dynasty. He restored much of the institutions of Sui's first ruler, Emperor Wen, reversing a number of changes that Emperor Yang made. He created Yang You the Duke of Xi, Li Jiancheng, his oldest son, was named
crown prince, while Li Shimin was made the Prince of Qin and Li Yuanji the Prince of Qi. Meanwhile, the Sui officials at Luoyang declared another grandson of Emperor Yang, Yang You's brother
Yang Tong, the Prince of Yue, as emperor, and refused to recognize the regime change in Chang'an. Emperor Gaozu's rule immediately faced a major challenge from
Xue Ju, an agrarian leader who had declared himself the Emperor of Qin. During the fall of 618, Xue took advantage of Li Shimin's illness to defeat an army commanded by Li Shimin and Liu Wenjing at Qianshui Plain (in modern
Xianyang,
Shanxi) and approach Chang'an. In response, Gaozu tried to enter an alliance with
Li Gui, the Prince of Liang, between whose domain and the Tang Xue's Qin state was located, writing Li Gui and referring to him as cousin. Li Gui briefly submitted to Gaozu. Meanwhile, before he could attack Chang'an, Xue Ju died of illness and was succeeded by his son
Xue Rengao, who was a capable commander but who had alienated his generals because of his cruelty. Li Shimin was soon able to attack Xue Rengao at Gaozhi (, in modern Xianyang as well), and force Xue Rengao to surrender. Meanwhile, Li Mi, having been defeated earlier in the year in a surprise attack (at the
Battle of Yanshi) by the Sui general
Wang Shichong, fled to Tang territory and submitted to Gaozu. Li Mi's general
Xu Shiji, who controlled a major part of Li Mi's former territory, also submitted, and Emperor Gaozu, impressed with Xu's faithfulness to Li Mi, bestowed the imperial surname of Li on Xu. Gaozu created Li Mi the Duke of Xing, but only made him the Minister of Feasts, a post that Li Mi viewed as below his stature. Around New Year 619, Li Mi requested Emperor Gaozu's permission to head east to persuade some of his former subordinates to submit to Tang, but once he left Chang'an, planned to restore his independence. He was ambushed and killed by the Tang general Sheng Yanshi (). In spring 619, Wang Shichong at Luoyang had Yang Tong yield the throne to him, ending the Sui dynasty and establishing a new state of Zheng. Around the same time, Li Gui, while stating that he wished to be a Tang subject, refused the Tang creation of Prince of Liang, instead declaring himself the Emperor of Tang. In summer 619, Li Gui's official An Xinggui (), formerly a Tang official, rebelled against Li Gui and captured him, submitting to Tang. Gaozu executed Li Gui and incorporated his domain into Tang. Also around the same time, the rebel leader
Du Fuwei, who controlled the modern southern
Anhui, submitted to Tang, and Gaozu also bestowed the imperial surname of Li on him, creating him the Prince of Wu. Similarly,
Luo Yi, who controlled the modern
Beijing region, submitted, was bestowed the imperial surname of Li, and was created the Prince of Yan. Meanwhile, Tang was facing another serious threat—Liu Wuzhou, now determined to march south against Tang. Emperor Gaozu sent Pei Ji against Liu's advancing army, but Pei was defeated by Liu, who then put Taiyuan under siege. Li Yuanji fled back to Chang'an, and much of modern Shanxi was seized by Liu. Emperor Gaozu then sent Li Shimin against Liu, and by summer 620, Li Shimin had defeated Liu, forcing him to flee to the Eastern Turks. Liu's territory was incorporated into Tang. Around the same time, however,
Dou Jiande the Prince of Xia made a major offensive against the cities that had submitted to Tang in modern
Hebei and
Henan, north of the Yellow River, seizing nearly all of them and taking Emperor Gaozu's cousin Li Shentong () the Prince of Huai'an, Emperor Gaozu's sister the Princess Tong'an, and Li Shiji's father Li Gai () captive. With Li Gai in Dou's custody, Li Shiji surrendered to Dou as well. In 620, Li Shiji, in association with another Tang general who surrendered to Dou, Li Shanghu (), plotted to ambush Dou, but the plot was discovered; Li Shanghu was killed, and Li Shiji fled back to Tang. In 620, Li Fuwei captured much of the territory of another agrarian ruler,
Li Zitong, the Emperor of Wu, in the lower
Yangtze River region, in the name of the Tang dynasty. Li Zitong, in turn, defeated and took over the territory of
Shen Faxing the Prince of Liang, roughly modern
Zhejiang. After Li Shimin defeated Liu, he started a campaign against Wang's Zheng state in fall 620. He initially could not decisively defeat Zheng, but by spring 621 had put the Zheng capital Luoyang under a tight siege, although he was not able to capture it. Wang sought aid from Dou. The latter agreed, concerned that a Tang victory over Zheng would also mean his own demise, but at the same time was eager to exploit the weakness of the Zheng and claim its domains for himself. Emperor Gaozu was initially fearful that Dou and Wang would be able to sandwich Li Shimin's forces between them and ordered Li Shimin to retreat, but upon Li Shimin's petition changed his mind and permitted Li Shimin to remain in the Luoyang region. Li Shimin, leaving Li Yuanji in charge of the siege of Luoyang, advanced and took up position at
Hulao Pass. In summer 621, the Tang and Xia forces
engaged at Hulao, and Li Shimin defeated Dou, capturing him. Despairing, Wang also surrendered, and most of the Zheng territory was seized by the Tang. Xia territory was also seized by Tang, but after Emperor Gaozu executed Dou, Dou's general
Liu Heita rose against the Tang and seized most of the former Xia territory, while
Xu Yuanlang, a rebel leader who had previously submitted to Zheng, also rose in revolt, occupying the modern
Shandong region. Also in 621, Li Xiaogong defeated
Xiao Xian the Emperor of Liang, who had controlled the modern
Hubei,
Hunan, and
Guangxi region, forcing Xiao Xian to surrender. On another front, Li Fuwei's lieutenant
Fu Gongshi defeated Li Zitong, forcing him to surrender as well. Liang and Wu territory were seized by Tang. Meanwhile, while not as noted as
Emperor Gao of Han's killing of
Han Xin and
Peng Yue, historians have nevertheless noted that some contributors to Emperor Gaozu's establishment of Tang were wrongly killed by him or killed based on fairly little evidence of wrongdoing: • Liu Wenjing, in 619, on accusation that he engaged sorcerers. • Emperor Gaozu's cousin Dugu Huai'en (), in 620, on accusation of treason. • Li Zhongwen () the Duke of Zhenxiang, in 620, on accusation of collaboration with Eastern Tujue. • Liu Shirang () the Duke of Yingyang, in 623, on accusation of collaboration with Eastern Tujue. ==Struggle between sons and the Xuanwu Gate Incident==