SD.remove(filename) or SD.rmdir(filename) not working

I want to remove an entire folder using SD.remove(filename) or SD.rmdir(filename) but but none of them is working.

The code :

SD card basic file example

This example shows how to create and destroy an SD card file
The circuit:

  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

Serial.print(“Initializing SD card…”);

if (!SD.begin(10)) {
Serial.println(“initialization failed!”);
Serial.println(“initialization done.”);

if (SD.exists(“SPOTLI~1”)) {
Serial.println(“SPOTLI~1 exists.”);
Serial.println(“Removing SPOTLI~1…”);
} else {
Serial.println(“SPOTLI~1 doesn’t exist.”);

// Check to see if the file exists:
if (SD.exists(“SPOTLI~1”)) {
Serial.println(“SPOTLI~1 exists.”);
} else {
Serial.println(“SPOTLI~1 doesn’t exist.”);


void loop() {
// nothing happens after setup finishes.

The output :

Initializing SD card…initialization done.
SPOTLI~1 exists.
Removing SPOTLI~1…
SPOTLI~1 exists.


Is SPOTLI~1 a file or a directory ?

I know little or nothing about the SD library, but it looks like in order to remove the SPOTLI~1 file you would need to be in the SPOTLI~1 directory but then you could not remove the SPOTLI~1 directory whilst still in that directory.

Itis a directory i have run the card info example provided by the SD library examples and here is a fragement of the output:
IF232017/ 2000-01-01 01:00:00
SPOTLI~1/ 2016-06-04 20:15:40
STORE-V2/ 2016-06-04 20:15:40
6878A9~1/ 2016-06-04 20:15:40
PSID.DB 2016-06-12 14:51:30 8192
TM~1.SNO 2016-06-04 20:15:40 0
TM~1.LIO 2016-06-04 20:15:40 0
LIO~1.CRE 2016-06-04 20:15:40 0
TMP.CAB 2016-06-04 20:15:40 0

Since i am in the root directory and the same goes for SPOTLI~1 is in the root so should not i be able to remove it ?

So, SPOTLI~1 is a directory. Why then use SD.remove() which deletes files ?

If you are in the root directory and SPOTLI~1 is an empty directory off the root then you should be able to remove it.

in the begining i only used rmdir but it didnt delete the directory (which is not empty as shown in the output ) so i added this remove which is a desperate move ..

in the begining i only used rmdir but it didnt delete the directory (which is not empty as shown in the output ) so i added this remove which is a desperate move ..

I suspect that rmdir() doesn't remove the directory BECAUSE it is not empty. Make the directory the current directory, and delete all the files in it, first. Then, make the directory's parent the current directory, and delete the directory.

From the reference page for the rmdir() function

Remove a directory from the SD card. The directory must be empty.

Hence my emphasis on an empty directory in reply #4