Emperor Daizong died in 779 and was succeeded by his son
Emperor Dezong. Later that year, Emperor Dezong made Zhang Yanshang the military governor of Xichuan Circuit (西川, headquartered in modern
Chengdu,
Sichuan) and the mayor of its capital Chengdu Municipality. (At the end of the campaign, however, there was a serious dispute between Zhang and Li Sheng — Li Sheng had, after the campaign, taken the military prostitute Gao Hong () with him. Zhang, angry with this, sent messengers to chase after Li Sheng's army to demand Gao back, thus causing a grudge between Li Sheng and Zhang.) In 783, there was a mutiny by his subordinate Zhang Ku (), who had been posted in the mountains in the west of Chengdu in defense against Tufan. Zhang Ku attacked Chengdu, forcing Zhang Yanshang to flee to Han Prefecture (漢州, in modern
Deyang,
Sichuan). It was only after another subordinate of Zhang Yanshang's, Chigan Sui (), attacked and killed Zhang Ku, that Zhang Yanshang was able to return to Chengdu. It was said that Zhang followed the law and ruled the circuit leniently, allowing the people of the circuit to recover from the wars that had ravaged the circuit ever since Anshi Rebellion. In 785, when the chancellor
Liu Congyi was seriously ill (and Liu would die later in the year), Emperor Dezong thus recalled Zhang to Chang'an, intending to make him an actual chancellor. Li Sheng, still displeased with Zhang, submitted a petition accusing Zhang of a number of crimes, and Emperor Dezong, not willing to go against Li's wishes, for the time being only made Zhang
Zuo Pushe (), one of the heads of the executive bureau (尚書省,
Shangshu Sheng). at the meeting site, Shang laid a trap for Hun and launched a sudden attack, killing and capturing many of Hun's attendants, but Hun escaped. When the news reached Chang'an, Emperor Dezong was so panicked that he considered fleeing Chang'an, but remained due to Li Sheng's advice. As a result of this debacle, Zhang claimed an illness and retired, while Emperor Dezong recalled Ma to the capital and stripped him of his command. Subsequently, the reduction in officer ranks was reversed under the advice of another chancellor,
Li Mi. Meanwhile, Zhang's rivalry with another official — Li Shuming () the military governor of Dongchuan Circuit (東川, headquartered in modern
Mianyang,
Sichuan) — which formed when they governed neighboring circuits, led Zhang to carry out some additional actions intended to bring down Li Shuming. At that time, Li Shuming's son Li Shēng (李昇, note different tone and character than the great general) was repeatedly seen visiting Emperor Dezong's aunt Princess Gao, whose daughter was the wife and crown princess of Emperor Dezong's son and
crown prince Li Song. He informed this to Emperor Dezong, intimating that Li Shēng was having an affair with Princess Gao. When Emperor Dezong asked Li Mi to investigate this, however, Li Mi correctly guessed that it was Zhang who informed Emperor Dezong of this and suggested no investigation, pointing out that Zhang's accusations also appeared to be intended to endanger Li Song. Emperor Dezong agreed, and thereafter moved Li Shēng to the position of head of Li Song's household, away from Princess Gao. Zhang died in winter 787 and was given posthumous honors. He was also given the posthumous name of Chengsu (成肅, meaning "successful and solemn"). His son
Zhang Hongjing later served as a chancellor during the reign of Emperor Dezong's grandson
Emperor Xianzong.
Ouyang Xiu, the lead editor of the
New Book of Tang, commented thus about Zhang: == Notes and references ==