diff --git a/include/libwebsockets/lws-esp32.h b/include/libwebsockets/lws-esp32.h
index e11d735e7b87b66d831da8f29022a94933d547b3..2d1c0f42bb7fd63166f7a36c6d116b6de56e68b8 100644
--- a/include/libwebsockets/lws-esp32.h
+++ b/include/libwebsockets/lws-esp32.h
@@ -109,7 +109,7 @@ static LWS_INLINE void uv_close(uv_handle_t *h, void *v)
 #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
-
+#define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
 
 /* user code provides these */
 
diff --git a/lib/plat/esp32/esp32-helpers.c b/lib/plat/esp32/esp32-helpers.c
index 9bd4cd4e7083c374e5d00a84d362ca4d1b4abfce..11eda871023106efeed6b7a96484b6ddd04d4952 100644
--- a/lib/plat/esp32/esp32-helpers.c
+++ b/lib/plat/esp32/esp32-helpers.c
@@ -1032,14 +1032,12 @@ lws_esp_ota_get_boot_partition(void)
 	                	}
 			}
 
-			/* destroy our OTA image header */
-			spi_flash_erase_range(ota->address, 4096);
-
 			/*
-			 * with no viable OTA image, we will come back up in
+			 * We send a message to the bootloader to erase the OTA header, we will come back up in
 			 * factory where the user can reload the OTA image
 			 */
 			lwsl_notice("  FACTORY copy successful, rebooting\n");
+			lws_esp32_restart_guided(LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA);
 retry:
 			esp_restart();
 		}