We investigate the possibility of the spontaneous breaking of CP symmetry in 4D SU(2) Yang-Mills at θ=π, which has recently attracted much attention in the context of the higher-form symmetry and the 't Hooft anomaly matching condition. Here we provide a numerical evidence that the CP symmetry is indeed spontaneously broken at low temperature and it gets restored above the deconfining temperature at θ=π, which is consistent with the anomaly matching condition and yet differs from the situation predicted in the large-N limit. We avoid the severe sign problem by performing simulations at imaginary θ. We obtain the critical temperature of the CP restoration and that of deconfinement at θ=π by analytic continuation, which leads to the above conclusion.