Flash erase is device specific. You generally can't just write values to flash, you need to use the flash controller to first erase a block, then perform write/verify accesses repeatedly till the value is stable. This write stage is generally automated by the controller too.
Since the flash is where code is generally run from, you also need to first copy your code download routine into ram, and execute from RAM whilst the flash is busy.
All of the accesses can be performed over the processor, or by an external debugger. If you want to perform the access by SWD, your debug software needs to be aware of the exact target device, it's memory map, and the requirements of the flash controller. All of this information should be in the datasheet for the device which you're using (and will be supported by the manufacturer's toolchain somewhere).
If you write your own flash routine, be sure to stick to the speficification. Flash is rather an analogue component, and even if you don't destroy the chip, you might still rather easily get to a state that is hard to recover from.