$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: removing a file from the SPIFFS file system

DFRobot Mar 14 2019 1066

In this esp32 tutorial we will check how to delete a file from the SPIFFS file system, using the ESP32 and the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this esp32 tutorial we will check how to delete a file from the SPIFFS file system, using the ESP32 and the Arduino core. You can read more about SPIFFS support on the ESP32 here.
The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start our code by including the SPIFFS.h library.

#include "SPIFFS.h"

After that, we will declare a helper function that allows to list all the files in the SPIFFS file system. With this function, we will be able to print the state of the file system after we create the file and after we delete it.

The detailed procedure for listing all the files on the SPIFFS file system can be seen on this previous tutorial.

void listAllFiles(){
 
  File root = SPIFFS.open("/");
 
  File file = root.openNextFile();
 
  while(file){
 
      Serial.print("FILE: ");
      Serial.println(file.name());
 
      file = root.openNextFile();
  }
 
}

Moving on to the setup function, we will start by opening a serial connection, so we are able to output the results of the program. After that, we will mount the SPIFFS file system, which is needed before we can start using it.

Serial.begin(115200);
 
if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}

Then we will create a file, so we are able to later delete it. If you already have some file in the file system that you can delete, you can use that file instead of creating a new one.

For a detailed guide on how to write a file on the SPIFFS file system, please check this previous tutorial.

In short, to create the file, we first need to call the open method on the SPIFFS object, passing as first input the name of the file and as second the opening mode. We will call the file “/test.txt” and we will pass the constant FILE_WRITE as opening mode. This method call returns an object of class File, which we will store in a variable.

Then we will check if the file was correctly opened and if it is, we will write some content to it using the print method. We will also check if the content was correctly written to the file or if some error has occurred during the procedure.

To finalize, we will call the close method on our File object.

File file = SPIFFS.open("/test.txt", FILE_WRITE);
 
if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
}
 
if (file.print("some content")) {
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}
 
file.close();

Now that we have created the new “test.txt” file, we will call the listAllFiles function to confirm it appears in the list.

Serial.println("\n\n---BEFORE REMOVING---");
listAllFiles();

After this, we will simply call the remove method of the SPIFFS object to delete the file. As input, this method receives the full path of the file: “/test.txt“.

As output, the method returns a Boolean value indicating if the file was successfully removed or not. We are not going to use the returned value in this tutorial because below we will print again the list of files.

SPIFFS.remove("/test.txt");

To finalize, as mentioned, we will print again the list of files in the SPIFFS file system, to confirm the file was indeed removed.

Serial.println("\n\n---AFTER REMOVING---");
listAllFiles();

The final code can be seen below.

#include "SPIFFS.h"
 
void listAllFiles(){
 
  File root = SPIFFS.open("/");
 
  File file = root.openNextFile();
 
  while(file){
 
      Serial.print("FILE: ");
      Serial.println(file.name());
 
      file = root.openNextFile();
  }
 
}
 
void setup() {
 
  Serial.begin(115200);
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
  File file = SPIFFS.open("/test.txt", FILE_WRITE);
 
  if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
  }
 
  if (file.print("some content")) {
    Serial.println("File was written");
  } else {
    Serial.println("File write failed");
  }
 
  file.close();
 
  Serial.println("\n\n---BEFORE REMOVING---");
  listAllFiles();
 
  SPIFFS.remove("/test.txt");
 
  Serial.println("\n\n---AFTER REMOVING---");
  listAllFiles();
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32 using the Arduino IDE. After the procedure finishes, open the Arduino IDE serial monitor.

You should get an output similar to figure 1, which shows the list of files both after the creation and after the deletion of the “test.txt” file. As can be seen, the file was first created and then removed from the file system, as expected.

Reference: https://techtutorialsx.com/2019/02/24/esp32-arduino-removing-a-file-from-the-spiffs-file-system/

REVIEW