Extract cleanup changes from #21793. (#21852)