How to upload esp8266 sketch with configuration of Erase flash


I’m trying to mimic the Arduino IDE option provided in Erase flash, Witch gives 3 different options :

  • Only Sketch
  • Sketch + WiFi Settings
  • All Flash Contents

I could found the corresponding lines in ESP8266/boards.txt. But couldn’t know how to add them to platformio.ini

I don’t know if this is Ok or not. please correct me.

build_flags =

I’m using nodemcuv2.


Close, but also not even close… :confounded: those are just the entries which add the menu option for erase…

You do want these lines…

but more specifically you want what’s after the upload.erase_cmd for the specific one you want… either sketch only (so no erase), Sketch + WiFi Settings (erase region rfcal), or erase all (erase flash).

Once you know that, you want to somehow add that to the upload command… there are two possible ways, not sure which one will actually work.

The easier option (if it works) is upload_flags = erase_region "{build.rfcal_addr}" 0x4000 or upload_flags = erase_flash as needed. However, since anything specified after upload_flags will be added to the end of uploader command, I’m concerned it may not work. So, Plan B is to use an extra_script based on the custom upload tool example to edit the upload command…


;ERASE_CMD="erase_region "{build.rfcal_addr}" 0x4000"

upload_protocol = env.subst("$UPLOAD_PROTOCOL")

if upload_protocol == "espota":
elif upload_protocol == "esptool":

… not forgetting to also add the following to your platformio.ini

`extra_scripts =

… assuming you called it, and that it’s located in the root folder of your project, i.e. next to the platformio.ini.

Note, I don’t have time to test this atm, so it may break… but it should get you started.

I don’t see why this can’t be added as a standard platformio.ini parameter in the future… who wants to volunteer to add it? :laughing: (thinking since he said it he’ll probably do it…)

1 Like

Thanks for this advanced starting point. @pfeerick

I’ve discovered a bit deeper based on your script :

We should extract “{build.rfcal_addr}” from the same source :

So replacing it with the actual address.

That follows the esptool command doc :

My platformio.ini :


platform = espressif8266@2.3.1

board = nodemcuv2

framework = arduino

build_flags =



lib_deps = Blynk

monitor_speed = 115200

upload_speed = 921600

upload_flags =

–erase_region 0x3FC000 0x4000

But gives me this error :

CURRENT: upload_protocol = esptool
Looking for upload port…
Auto-detected: COM3
Uploading .pio\build\nodemcuv2\firmware.bin
usage: esptool [-h] [–chip {auto,esp8266,esp32}] [–port PORT] [–baud BAUD]
[–before {default_reset,no_reset,no_reset_no_sync}]
[–after {hard_reset,soft_reset,no_reset}] [–no-stub]
[–trace] [–override-vddsdio [{1.8V,1.9V,OFF}]]

esptool: error: argument operation: invalid choice: ‘–erase_region 0x3FC000 0x4000’ (choose from ‘load_ram’, ‘dump_mem’, ‘read_mem’, ‘write_mem’, ‘write_flash’, ‘run’, ‘image_info’, ‘make_image’, ‘elf2image’, ‘read_mac’, ‘chip_id’, ‘flash_id’, ‘read_flash_status’, ‘write_flash_status’, ‘read_flash’, ‘verify_flash’, ‘erase_flash’, ‘erase_region’, ‘version’)
*** [upload] Error 2

I guess the issue comes out at over-riding options ? Or am I doing it wrong ?

By using as you mentioned, I’ve got another error :

my :



ERASE_CMD=“erase_region 0x3FC000 0x4000”

upload_protocol = env.subst("$UPLOAD_PROTOCOL")

if upload_protocol == “espota”:
elif upload_protocol == “esptool”:

error msg :

Uploading .pio\build\nodemcuv2\firmware.bin
usage: esptool write_flash [-h] [–erase-all]
[–flash_freq {keep,40m,26m,20m,80m}]
[–flash_mode {keep,qio,qout,dio,dout}]
[–flash_size FLASH_SIZE]
[–spi-connection SPI_CONNECTION] [–no-progress]
[–verify] [–encrypt]
[–compress | --no-compress]
[ …]
esptool write_flash: error: argument : Address “.pio\build\nodemcuv2\firmware.bin” must be a number
*** [upload] Error 2