Should You Erase the ESP32 Flash Memory in Arduino IDE?

Esp32 Dip Chip Closeup Feature Image

Recently, I got into a mess with the Arduino IDE going wonkers with a variable on my NodeMCU32-S. I wanted to make a Wi-Fi access point with it, but it just wouldn’t change the variable for the SSID name.

Turns out, it still had data from an old project of mine when I used it for the same purpose a few months back.

In a way, this was a discovery for me. Never did I know that the IDE had an option to keep old data in flash memory. I thought it wiped everything, just as it always did with Arduino boards.

What’s the Flash Memory?

The flash memory is where your ESP32 stores the information it needs to access the Wi-Fi and Bluetooth modules, plus the bootloader that processes your code. It also has a lot of space reserved for your code. This is the place where it stores the “10101” version of whatever you typed in the IDE.

This flash memory is located in a chip over your board. And the size of this memory varies. Usually, it comes in 4MB, 8MB, and 16MB variants. That applies to all the kinds of boards that the ESP32 is placed on.

How to Clear the Flash Memory

On the Arduino IDE, you can erase the flash memory for the ESP32 by going to “Tools -> Erase All Flash Before Sketch Upload.”

Arduino Ide How To Automatically Erase Flash Memory Before Sketch Upload Screenshot

By default, this is set to Disabled – and you would have never had seen this setting at all if you didn’t have an ESP32 board connected on your computer! If you’re new to using the ESP32 with the Arduino IDE, you’ll need to give this setting a little thought.

Should You Automatically Erase the Flash Memory?

There are only two reasons for why you should automatically erase the flash memory.

First is while you’re prototyping. You are better off with a clean slate of flash memory every time you test your code or circuit.

By erasing the flash memory every time you upload code into the ESP32, you are reducing the number of variables to think about while looking for what went wrong within your circuit or system.

But then again, not all data in the flash memory are unimportant. Sometimes, you’d want to have a set of sensor data or custom variables in there that you could access anytime without having to flash them in all the time. That’s why the second reason is to do this only if you don’t have anything important in the flash memory.

Why You Should Not Delete the Flash Memory

So what kind of oh-so-important information could be in there that you wouldn’t want to remove?

Turns out, there’s a lot. You could have dummy sensor data preloaded into the flash memory. Another would be custom variables – these are usually char arrays that make up a string of characters. You could store anything in there – be it SSIDs, passwords, or even entire paragraphs!

Arduino Ide Variables And Code Screenshot
Arrays of characters can be stored in flash memory, making it easier to store information without having to upload them every single time. Just imagine how things would be better if that were applied here!

The greatest reason why shouldn’t be doing this is because it takes a while to clear up the flash memory. Even if it only adds 10-20 seconds every time, that extra time would add up to minutes once you’re doing frequent edits to your code. You could have lost up to an hour in total by the time you’re done with your project.

Also, the current version of the Arduino IDE still doesn’t support MicroPython. This means you will still have to compile and upload your code every time you make an edit. If this code has a rather large amount of custom variables in it, your PC will have to delete and rewrite the same code over and over and over.

That wastes time and electricity. So, in a way, the flash memory in the ESP32 is still useful – but only if you know how to use it.

Frequently Asked Questions

Will deleting the ESP32 flash memory in Arduino IDE remove the bootloader?

It won’t. The bootloader (and all the other important stuff like the WiFi and Bluetooth drivers) are all hidden in protected areas of the flash memory. The Arduino IDE does not have access to these areas, so it can never remove them.

Does erasing the ESP32 flash memory in Arduino IDE reduce the flash memory's lifespan?

In a way, you are technically reducing the lifespan of the flash memory by a slight amount whenever you delete and rewrite information into the flash memory. But that only affects the areas that already had data in them in the first place. If there weren’t much data at all, you might not even notice any difference in the flash memory’s lifespan.

What happens when the ESP32 bootloader is removed or overwritten?

When the bootloader is removed, the ESP32 got bricked in the same way that phones get bricked when you root them. It won’t accept new code and it might not even know how to read the code that’s already been uploaded into it. Best case, it’s a soft brick and you can still repair the damage. Worst case, however, and the ESP32 doesn’t work at all. It’s become a rather expensive brick – or an expensive wafer, if you will.

Image credit: Wikimedia Commons. Screenshots by Terenz Jomar Dela Cruz

Subscribe to our newsletter!

Get the best of IoT Tech Trends delivered right to your inbox!

Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to make the most awesome robot the world has ever seen! He would have done that already if he wasn't so busy burning through LEDs.