Do I have to erase everything in the disk first and the install OS X Mavericks?
That's how we used to go about it, but if you erase the disk you may not be able to install Mavericks any more. Apple removed the ability to download and install certain older versions of macOS / OS X through their Recovery servers a few months ago (May 2023).
Back up your Mac: Back up your files with Time Machine on Mac - Apple Support. That will at least give you the ability to reinstall whatever macOS version is running on that Mac, if you decide to erase it.
Consider creating a "bootable USB installer" also: Create a bootable installer for macOS - Apple Support. There are quite a few caveats associated with it so study that document carefully.