If it’s in Safe Mode, you should be able to flash new code to it. Photons should automatically update their system firmware to match the user one you’ve selected, whereas an Electron won’t auto-update, leaving you in Safe mode if user firmware > System firmware.
With a Photon, flash new code.
With an Electron, flash code with a version <= system version, or update the system firmware. Using the CLI, in DFU mode, that should be ‘particle update’, assuming you’re on the latest CLI version.