Remove target from MAKECMDGOALS?

You cannot do what you want here as far as I know.

You can modify the variable value and you will see the changes if you check the value yourself but modifying the value of MAKECMDGOALS will not affect make in any way.

If you look at Appendix A Quick Reference in the GNU Make Manual you will see that it says:

MAKECMDGOALS

The targets given to make on the command line. Setting this variable has no effect on the operation of make.

See Arguments to Specify the Goals.

The closest you could get to what you are trying to do here, I think, would be to re-execute make on the Makefile (or whatever) manually.

That being said this sort of thing is probably better done as variables instead of targets.

$ cat Makefile
$(info $(origin UBSAN))
$ make
undefined
$ make UBSAN=
command line

So something like this.

# Undefined Behavior Sanitzier (Clang and G++)
ifneq (undefined,$(origin UBSAN))
CXXFLAGS += -fsanitize=undefined
endif # UBsan