Skip to content

ESP32 NVS Write Does Not Persist

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 NVS Write Does Not Persist. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 NVS write appears successful but data is lost after restart.

Quick Fix

Wrong

nvs_set_i32(handle, "counter", 42);  // No commit!
Data written but lost after reset. Reads return old or default value.
nvs_handle_t handle;
nvs_open("storage", NVS_READWRITE, &handle);
nvs_set_i32(handle, "counter", 42);
nvs_commit(handle);  // REQUIRED to persist
nvs_close(handle);
Serial.println("Counter saved to NVS");
Counter saved to NVS
(Value persists across reboot)

Prevention

Always call nvs_commit() after writes to flush to flash. Open with NVS_READWRITE mode for writes. Write in batches and commit once. Do not commit too frequently (flash wear). Handle ESP_ERR_NVS_INVALID_HANDLE.

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

FAQ

### How many NVS writes can I do?

NVS uses flash memory rated for ~100,000 write cycles. Frequent writes (every second) will wear out the flash within days.

Is nvs_commit() required?

Yes. Without commit, changes stay in a cache and are lost on power loss. Commit flushes the cache to flash.

Can I write strings to NVS?

Yes. Use nvs_set_str(handle, key, value). Max string length is 4000 bytes. Use nvs_set_blob() for binary data.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro