The plot, however, was revealed by his staff member Ru Kuan (茹寬) to Wei, and when Yuwen Liang attacked Wei, Wei was prepared and defeated him. Yuwen Liang was killed in battle, and Emperor Xuan, then carrying the atypical title "Emperor Tianyuan" (天元皇帝, Tianyuan Huangdi) as retired emperor, then executed Yuwen Wen.
The plot, however, was made known by Ru Kuan to Wei, and when Yuwen Liang launched a surprise assault on Wei, Wei was able to defend himself and emerge triumphant. Yuwen Liang lost his life in battle, and Emperor Xuan, who had taken on the unusual title of "Emperor Tianyuan" during his retirement, then ordered the execution of Yuwen Wen.