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.
In 618, during the spring season, General Yuwen Huaji staged a successful coup against Emperor Yang, installing Yang Hao as the ruler of Qin. Notably, he eliminated many prominent officials and members of the imperial Yang family, but spared Yang Gongren's life and appointed him to oversee civil affairs.