In theory you’d just apt remove *package name* and then follow up with sudo apt-build-essential, the challenge is identifying all the packages you’ve installed that are GCC9 related to remove them.
Debian/Ubuntu has a complex setup where multiple packages can coexist so it might be enough to just install build-essential directly and it might replace GCC9 with GCC11 as default but most of the time builds work better when done in virtual machines or container environments to help trivialise swapping things out like this and lowers the complexity of managing potentially incompatible build tools.