Home / References / ESP32 Library / WiFi API / WiFiScan
Description
The WiFi.scanDelete
method in the ESP32 WiFi Library is used to clear the results of a previous Wi-Fi network scan from memory. When you perform a scan using WiFi.scanNetworks()
, the ESP32 stores the list of detected access points (APs). Calling WiFi.scanDelete
frees up this memory, ensuring efficient resource management in your IoT projects. This method is particularly useful when you need to refresh scan data or manage memory in long-running applications.
Syntax and Usage
The WiFi.scanDelete
method is straightforward and does not accept any arguments. It simply clears the stored scan results. Below is the syntax and its typical usage:
WiFi.scanDelete();
Since this method has only one usage scenario (without arguments), it can be called as follows:
- Without Arguments: Use
WiFi.scanDelete()
after completing a Wi-Fi scan to release the memory allocated for storing scan results. This is the standard and only way to invoke the method.
Argument(s)
This method does not require any arguments.
Return Value
The WiFi.scanDelete
method does not return any value. It simply performs the action of clearing the scan results from memory, and its success is implicit in its execution. After calling this method, subsequent calls to WiFi.scanComplete()
will return -2
(indicating no scan is in progress or stored), and WiFi.SSID(i)
will no longer provide access to the previous scan data.
Example Codes
Example 1: Basic Usage After Scanning Networks
This example demonstrates how to use WiFi.scanDelete
to clear scan results after scanning for nearby Wi-Fi networks. It’s ideal for understanding the method’s role in memory management.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use WiFi.scanDelete to clear scan results after scanning
* for nearby Wi-Fi networks. It’s ideal for understanding the
* method’s role in memory management.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers,learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Starting WiFi scan...");
int networksFound = WiFi.scanNetworks(); // Scan for networks
if (networksFound > 0) {
Serial.print(networksFound);
Serial.println(" networks found:");
for (int i = 0; i < networksFound; i++) {
Serial.print(i + 1);
Serial.print(": ");
Serial.println(WiFi.SSID(i));
}
} else {
Serial.println("No networks found.");
}
// Clear the scan results
WiFi.scanDelete();
Serial.println("Scan results cleared from memory.");
}
void loop() {
// Nothing to do here
}
Example 2: Periodic Scanning with Memory Cleanup
This example shows how WiFi.scanDelete
can be used in a loop to periodically scan networks and clean up memory, which is useful for continuous monitoring applications like IoT sensors.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how WiFi.scanDelete
* can be used in a loop to periodically scan networks and clean
* up memory.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers,learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup complete. Starting periodic scanning...");
}
void loop() {
Serial.println("Scanning WiFi networks...");
int networksFound = WiFi.scanNetworks(true); // Asynchronous scan
while (WiFi.scanComplete() == -1) {
delay(100); // Wait for scan to complete
}
networksFound = WiFi.scanComplete();
if (networksFound > 0) {
Serial.print(networksFound);
Serial.println(" networks found:");
for (int i = 0; i < networksFound; i++) {
Serial.print(i + 1);
Serial.print(": ");
Serial.println(WiFi.SSID(i));
}
} else {
Serial.println("No networks found or scan failed.");
}
// Clear the scan results to free memory
WiFi.scanDelete();
Serial.println("Scan results cleared. Waiting for next scan...");
delay(5000); // Wait 5 seconds before scanning again
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 AsyncUDP Librarry
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!