Skip to content

ESP32 NVS Erase Key Does Not Clear Data

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 NVS Erase Key Does Not Clear Data. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 NVS erase_key() returns success but the key still exists when read.

Quick Fix

Wrong

nvs_erase_key(handle, "temp");  // Don't forget to commit
nvs_get_i32(handle, 'temp', &val) still returns old value.
esp_err_t err = nvs_erase_key(handle, "temp");
if (err == ESP_OK) {
  nvs_commit(handle);  // Must commit erase
  Serial.println("Key erased");
} else if (err == ESP_ERR_NVS_NOT_FOUND) {
  Serial.println("Key did not exist");
} else {
  Serial.printf("Erase failed: %s\n", esp_err_to_name(err));
}
// Verify
err = nvs_get_i32(handle, "temp", &val);
if (err == ESP_ERR_NVS_NOT_FOUND) {
  Serial.println("Verified: key deleted");
}
Key erased
Verified: key deleted

Prevention

Call nvs_commit() after nvs_erase_key(). Check return code for ESP_ERR_NVS_NOT_FOUND. Verify deletion by attempting to read the key. Erase all keys in a namespace with nvs_erase_all(handle).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### How do I erase all keys in a namespace?

Call nvs_erase_all(handle) followed by nvs_commit(handle). This clears all key-value pairs in that namespace.

Does erasing recover NVS space?

Yes. Erased entries become available for new writes. The space is reclaimed on the next commit.

Can I recover an erased key?

No. NVS erase is permanent. If you need versioning, store a revision number alongside the data.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro