How to erase flash?

Brief - How do I erase all the flash?

Detail - I moved up from developing ESP8266 devices from the Arduino environment for about a week now. I’m now using Visual Studio Code, PlatformIO. I was generally amazed at how everything worked fine first time. I compiled and run the sample programs within five minutes of finishing the install process. Over the last week, I’ve been porting some of my programs over. Everything has worked fine. I have discovered that the upload process must only clear the program flash area. I’ve had the program display all 4MB of the flash memory on a ESP8266 WeMos board and it is quite cluttered from its time being used in the Arduino environment.

I’ve done a search on the Internet. Many links land on this forum. All suggestions either don’t work for me or they’re talking about some files deep in the bowls of PlatformIO. I’ve tried…

  1. In the UI, there is an “Erase Flash” option under PROJECT TASKS/d1_mini/Platform/Erase Flash - This returns an error ***** [erase] Error 1** and lots of details, but finally says: “serial.serialutil.SerialException: could not open port ‘COM5’: PermissionError(13, ‘Access is denied.’, None, 5)” This, even though the project uploads/runs just fine.

  2. Tried this It suggests that simply doing pio run --target erase should do what I need. This is what I get when I try:

PS C:\Users\user\Documents\PlatformIO\Projects\InqPortal> pio run --target erase
*pio : The term ‘pio’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct *
and try again.
At line:1 char:1
+ pio run --target erase
+ ~~~

    • CategoryInfo : ObjectNotFound: (pio:String) [], CommandNotFoundException*
    • FullyQualifiedErrorId : CommandNotFoundException*

I confirmed that its running the newest as of last week.

I know I must be missing something simple as this is a fundamental need. Thanks for any help you can provide.

You have installed PlatformIO IDE, into VSCode I assume? If so, then the command line options are not on your %PATH%. You will need to follow the Windows instructions on installing the shell commands here.

Once done, open a new command line session and you should find that the pio commands “just work”. :wink:



Thank you for responding.
The tutorial I used mentioned those and I thought I’d used them, but to be sure, I uninstalled both Visual Studio Code and Python… and then re-installed and made sure “adding to path” was checked in both install programs.

I double checked the path and it appears to have valid paths added.
PATH=D:\Program Files\Python310\Scripts;D:\Program Files\Python310;D:\Program Files\Microsoft VS Code\bin

I loaded my project and get the same errors trying the Project Task and the pio run --target erase.

Since you suggested that pio is just a program on a path versus some embedded process within VSC or Python, I did a search on my computer. I found pio.exe in another folder that was not on the path: C:\Users\Dennis.platformio\penv\Scripts

I added this path to my PATH environment variable and re-launched VSC. From the powershell inside VSC, I get the same error. Its as if it doesn’t get the Environment PATH.

I brought up a separate Command Prompt, moved to my project’s directory and tried pio run --target erase, and at least I get something different:

C:\Users\Dennis\Documents\PlatformIO\Projects\Memory>pio run --target erase
Processing d1_mini (platform: espressif8266; board: d1_mini; framework: esp8266-

Verbose mode can be enabled via `-v, --verbose` option
PLATFORM: Espressif 8266 (3.2.0) > WeMos D1 R2 and mini
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
 - framework-esp8266-nonos-sdk 2.1.0
 - tool-esptool 1.413.0 (4.13)
 - tool-esptoolpy 1.30000.201119 (3.0.0)
 - toolchain-xtensa 1.40802.0 (4.8.2)
LDF: Library Dependency Finder ->
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Looking for serial port...
Auto-detected: COM5
Erasing... v3.0
Serial port COM5
Traceback (most recent call last):
  File "C:\Users\Dennis\.platformio\packages\tool-esptoolpy\", line 39
69, in <module>
  File "C:\Users\Dennis\.platformio\packages\tool-esptoolpy\", line 39
62, in _main
  File "C:\Users\Dennis\.platformio\packages\tool-esptoolpy\", line 35
51, in main
    esp = chip_class(each_port, initial_baud, args.trace)
  File "C:\Users\Dennis\.platformio\packages\tool-esptoolpy\", line 27
1, in __init__
    self._port = serial.serial_for_url(port)
  File "C:\Users\Dennis\.platformio\penv\lib\site-packages\serial\",
line 90, in serial_for_url
  File "C:\Users\Dennis\.platformio\penv\lib\site-packages\serial\
", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr,
serial.serialutil.SerialException: could not open port 'COM5': PermissionError(1
3, 'Access is denied.', None, 5)
*** [erase] Error 1
========================== [FAILED] Took 0.83 seconds ==========================

Any ideas?
Thanks again.

OH! I rationalized that the command prompt window couldn’t get access to COM5 because VSC was still running and I had uploaded the program and run it. So it still had COM5 busy.

I closed it and re-ran the line in the command prompt. IT WORKED!

This works for me, but it feels kind of a kludge! Any ideas why this might not work inside VSC/Python/PlatformIO?

Thanks for your help.

OK, I’m starting to figure out what’s going on. Inside the VSC , powershell tab, I pasted the command with the full path pre-pended. That gave me the COM5 Access is denied error. I noted that my running program was being monitored…

… and guessed that VSC doesn’t have the ability to stop that and process the erase command. So, I deleted that step and rerun the command and that worked.

Even better… now that I know that the Monitor can’t be running, I stopped it, the Erase Flash Project Task now works.

Thanks for your help… it got me to what I needed.

1 Like

I’m glad you got sorted out, I’m not a Windows guru I’m afraid, I use Linux myself.

In VSCode, there’s an option, Terminal->New Terminal which does just that, gives you a terminal. However, it will not have the command line commands available, unless they are on your path.

When you have a project open, the PlatformIO Ant/Alien head appears at the left side toolbar. Click it and you have a few options, but one is “Quick Access”. Open that option, then choose “Miscellaneous” and “New Terminal”.

Now you have the command line commands even if not on your path.

Withing VSCode and PlatformIO, when you click to open the serial monitor (looks like a US power plug on the bottom toolbar) this opens a monitor on the same port as you upload. Normally, at least on Linux, if I choose to recompile and upload the program, PlatformIO closes the monitor connection and does the upload, before opening the monitor again. (Or, appears to!) However, if I have any other application on the same port, I can’t do this.

I’m assuming Windows does something similar and possibly using PowerShell instead of “normal” commandline (cmd.exe) as your shell within VSCode is not helping? There’s an option to change the default shell/terminal in VSCode.

Ok, quick test.

I opened an UNO project and compiled and uploaded the code. Then started the Monitor. The output appeared as expected. I then opened a new PlatformIO Terminal as detailed above and when I tried uploading again, I got errors. The upload failed.

Closing the Monitor and trying again all worked. So it looks like the Monitor was your problem.

However, if I closed all the open terminal sessions within VSCode, then compiled my code again, this time with the bottom toolbar, (The Tick/Check mark icon) then uploaded it (The right pointing arrow) and opened the monitor using the toolbar, not the drop down, (US power plug) The monitor opens as usual and the output is displayed.

Now when I leave the monitor open and click to upload again, the monitor is indeed closed and reopened after the upload. So, using the bottom toolbar seems the best bet!


I apologize. I’m just found this reply. I didn’t have forum notifications set properly. Now fixed.

Thank you for these details. Before, I would have thought that the whole project was suppose to act the same on all systems, but your point reminded me that this VSCode/PlatformIO does have to use OS specific programs under the covers. I’ll definitely keep that in mind going forward when I run into differences.

In this particular case, I was used to the Arduino IDE selectively erasing every time and that the IDE disables the monitor on its own. Now that I know what the behavior is of VSCode/PlatformIO, I like this better. I just have to remember to delete the monitor window when its showing program output.

No apology necessary.

If you only use the compile, upload and monitor buttons on the bottom toolbar, you will see that PlatformIO automatically closes the monitor when you select upload. It will then reopen the monitor again after the upload finishes. No need to manually close it.


Yes, I’m familiar with this now. In fact, that was the problem for me… it’s automatic for compiling, erasing program flash and then re-opening the monitor, but it’s not automatic for erasing the rest of the flash via the Project Tasks / Erase Flash option as it is in Arduino IDE. Once I really thought about the specific error message and realized there were multiple processes vying for the COM port, it became obvious what the problem was.

Is there some way I can add an Erase button on the bottom toolbar and/or make it kill the monitor process? The Settings page is so vast and comprehensive, its really difficult to find little things that I’m used to finding in the regular Visual Studio.

Still getting my feet wet.

Ah! My apologies, I misunderstood. (Again! :wink:)

Not that I’m aware of, sorry, but there might be one, given that PlatformIO’s extension to VSCode adds buttons. Maybe @maxgerhardt will chime in here – he knows everything!


  1. Not easily. The way I see it, the PlatformIO VSCode extension code defines the toolbar

but there is no way to influence the toolbar through the platformio.ini or Advanced Scripting. You can add custom targets that will appear in the PlatformIO project task list, however, “Erase Flash” is already exactly that (in the platform category), so there’s no point in doing that.

  1. For the automatic closing of any monitor tabs, that looks again to me as if if it’s controlled by the extension code. You could open an issue at Issues · platformio/platformio-vscode-ide · GitHub to have that addressed.

Thank you, but I think it would be better for me to get more comfortable with VSCode/PlatformIO before trying to influence it’s direction.

LOL… like the saying goes, we’re two divided by a common language (and pond)… unless you speak native Gaelic. Then I’d have to say your English is far better than my Gaelic. :rofl:

I am Scottish, yes.
I do not speak Gaelic – I was born on the North East coast of Scotland, Gaelic is mostly from the West and the Islands.
I have a smattering on Italian as my late step father was Italian and a chef! I can order two glasses of white wine, and two ice creams! :grin: