installer remove file with upgrade specific version

I use cmake to generate the installer.
Assume the installer version:
cmake-3.24.0-windows-x86_64.msi
cmake-3.23.3-windows-x86_64.msi
cmake-3.21.3-windows-x86_64.msi

My computer cmake current version is cmake-3.23.3 .
I want to upgrade it to cmake-3.24.0.
I hope to the installer do check current cmake version and remove some file. Only 3.23.3 → 3.24.0 do remove some file.

How to do check current version and installing version?
How to remove file?

If it has the command sample, it will be good
Thanks a lot.

PS