If you need to clear EEPROM on an ESP32, the right method depends on how your sketch stores data. Unlike older Arduino boards, the ESP32 does not have dedicated EEPROM hardware. The Arduino EEPROM library instead emulates EEPROM using flash storage, while many ESP32 projects use the Preferences library or ESP-IDF’s Non-Volatile Storage (NVS) directly. This guide explains how to reset each option, how to avoid erasing unrelated data, and how to check that the reset worked.
What “EEPROM” Means on an ESP32
EEPROM is memory that keeps its contents when power is removed. On some microcontrollers, it is a separate type of memory. The ESP32, however, stores persistent data in flash. When you use the Arduino-ESP32 EEPROM library, the library provides an EEPROM-like interface and saves its data in a flash-backed area.
This distinction matters because clearing one kind of stored data does not necessarily clear another. For example, your sketch’s EEPROM-emulated values, settings saved with Preferences, Wi-Fi credentials, and other NVS entries may be stored separately. Before erasing anything, identify which library or API your code uses. Look for calls such as EEPROM.begin(), Preferences.begin(), or ESP-IDF functions beginning with nvs_.
Clear Arduino EEPROM Emulation on ESP32
If your sketch includes EEPROM.h and calls EEPROM.begin(), clear the emulated EEPROM by writing a chosen value to every address and then calling EEPROM.commit(). The commit is important: without it, the changes may only exist in memory and may be lost when the board restarts.
The following example writes 0xFF to every byte. This is a common reset value because erased flash bytes typically read as 0xFF. Your application must still treat that value as “unset” or invalid where appropriate.
#include <EEPROM.h>
const size_t EEPROM_SIZE = 512;
void clearEeprom() {
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("EEPROM initialization failed");
return;
}
for (size_t address = 0; address < EEPROM_SIZE; address++) {
EEPROM.write(address, 0xFF);
}
if (EEPROM.commit()) {
Serial.println("EEPROM data cleared");
} else {
Serial.println("EEPROM commit failed");
}
EEPROM.end();
}
void setup() {
Serial.begin(115200);
delay(500);
clearEeprom();
}
void loop() {
}
Use the same size in EEPROM.begin() that your application uses. The size determines the available emulated address range; it is not a command to erase the entire ESP32 flash. If your project stores a structure with EEPROM.put(), make sure the selected size is large enough for that structure and any other stored values.
You can run the reset code once from setup(), or place it behind a button or a deliberate command. Avoid leaving it in a function that runs on every boot unless you intend to erase the data every time the ESP32 starts.
Should You Clear EEPROM to 0xFF or 0x00?
Choose the reset value based on what your program expects. Writing 0xFF is useful when your code recognizes all-ones data as an unused or invalid value. Writing 0x00 may be more convenient when your program expects settings to start at zero. Neither value is automatically correct for every application: a value that looks like a valid setting can cause confusing behavior if the program does not check for an initialization marker.
For example, a robust configuration format can include a version number or signature. On startup, the program checks that marker. If it is missing or incorrect, the sketch loads safe defaults and saves a valid configuration. This is often more reliable than assuming that every cleared byte has a useful value.
To fill the EEPROM-emulated area with zeroes, change the write line in the example to:
EEPROM.write(address, 0x00);
Reset Values Saved with the Preferences Library
If your code uses Preferences.h, it is working with named NVS namespaces and keys rather than the Arduino EEPROM interface. To remove the keys in one namespace, open that namespace in read-write mode and call clear(). This is usually a better choice than erasing the whole NVS partition because it targets the settings managed by that part of your application.
#include <Preferences.h>
Preferences preferences;
void clearSettings() {
if (!preferences.begin("app-settings", false)) {
Serial.println("Could not open preferences");
return;
}
bool cleared = preferences.clear();
preferences.end();
if (cleared) {
Serial.println("Settings namespace cleared");
} else {
Serial.println("Could not clear settings");
}
}
void setup() {
Serial.begin(115200);
delay(500);
clearSettings();
}
void loop() {
}
Replace "app-settings" with the namespace your sketch actually uses. The namespace name must match the one passed to Preferences.begin(). Calling clear() removes the keys in that namespace; it does not mean “erase every setting stored on the ESP32.” If you only want to reset one setting, use preferences.remove("keyName") instead, substituting the actual key name.
After clearing a namespace, your application should handle missing keys. For example, when reading a setting, provide a sensible default value so the sketch can start normally even when no saved value exists.
Erase the NVS Partition with ESP-IDF
For an ESP-IDF application that manages NVS directly, nvs_flash_erase() erases the NVS partition. This is broader than clearing one Preferences namespace: it can remove unrelated values stored in that partition, including settings used by other parts of the application. Use it only when you intend to reset NVS as a whole.
#include "nvs_flash.h"
void resetNvs() {
esp_err_t result = nvs_flash_erase();
if (result == ESP_OK) {
result = nvs_flash_init();
}
if (result == ESP_OK) {
printf("NVS erased and initialized\n");
} else {
printf("NVS reset failed: %s\n", esp_err_to_name(result));
}
}
In an ESP-IDF application, NVS initialization is normally performed during startup. If initialization reports that the stored NVS format is incompatible or the partition needs to be erased, follow the recovery approach appropriate for your project and ESP-IDF version. Do not add an unconditional erase to ordinary startup code: it would erase persistent NVS data on every boot.
Clear EEPROM or NVS from the Arduino IDE
If you want to reset the device rather than add reset code to your sketch, the Arduino IDE’s erase option may be suitable. Depending on the Arduino-ESP32 core version and board configuration, the Tools menu may include an option such as “Erase All Flash Before Sketch Upload.” Enabling it and uploading a sketch can erase stored flash data before the upload.
This is a broad operation, not a targeted EEPROM reset. It may remove other data stored in flash, including NVS settings or filesystem contents, and its exact effect depends on the board package and partition configuration. Check the option’s description for your installed core version, and back up anything important before using it. When you only need to clear one application setting, clearing the relevant EEPROM range or Preferences namespace is safer.
Erasing flash is also different from uploading a new sketch in the usual way. A normal upload may replace the program without clearing all persistent data, so old settings can remain and affect the new firmware. If the new sketch behaves as if it remembers an earlier configuration, determine where that configuration is stored before choosing a reset method.
How to Verify That the Data Was Cleared
After clearing storage, restart the ESP32 and read the values back. For EEPROM emulation, print selected addresses or use the same EEPROM.get() or EEPROM.read() calls used by your application. For Preferences, check whether a key exists or read it with a default value. Verification helps distinguish a successful reset from an uncommitted write or a mismatch between the namespace, key, or EEPROM size.
- Confirm that the clear function ran by checking Serial Monitor output.
- Check that
EEPROM.commit()returned success after writing EEPROM-emulated data. - Make sure the namespace and key names match the ones used when saving Preferences data.
- Power-cycle the board and read the values again to confirm they remain reset.
- Check that startup code is not immediately writing the old values back.
Common Problems When Clearing ESP32 EEPROM
The old data is still there. You may be clearing the wrong storage system. For example, clearing EEPROM emulation does not necessarily clear a Preferences namespace. Search the project for the code that writes the setting, then reset storage through the same API.
The reset works until power is removed. With Arduino EEPROM emulation, check that your code calls EEPROM.commit() after writing. Without that call, the changes may not be saved to flash.
The sketch starts with strange values. Your program may be interpreting 0xFF or another reset value as a valid setting. Validate stored data before using it, and load defaults when the data is missing, out of range, or has an unexpected format.
The ESP32 keeps resetting its settings. Make sure your clear function is not called unconditionally on every boot. Add a one-time trigger, such as a button held during startup, a temporary test flag, or a command received over Serial. Remove the trigger after confirming the reset works.
Other data disappeared too. A full flash or NVS erase can affect more than the settings you intended to reset. For future resets, prefer writing to the specific EEPROM-emulated range, removing a single key, or clearing only the relevant Preferences namespace.
Conclusion
To clear EEPROM on an ESP32, first identify how your project stores its data. For Arduino EEPROM emulation, write a reset value to each address and call EEPROM.commit(). For the Preferences library, clear the specific namespace or remove individual keys. Use nvs_flash_erase() or an IDE flash erase only when you deliberately want a broader reset. Verify the result after restarting, and ensure your application can safely load defaults when saved values are missing.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.