In spring 618, the general Yuwen Huaji killed Emperor Yang in a coup and declared Emperor Yang's nephew Yang Hao the Prince of Qin emperor. While he killed a large number of high level officials and imperial Yang clan members, he did not kill Yang Gongren and made Yang Gongren the minister of civil service matters.
This event occurred in the spring of 618 when General Yuwen Huaji took control by overthrowing Emperor Yang and proclaiming Yang Hao as the ruler of Qin. He carried out significant purges, eliminating many prominent officials and members of the imperial Yang clan, but spared Yang Gongren's life and appointed him to a crucial role.