SDfat, i cant iteratively delete files

Hello community, im a beginner with arduino and currently implementing my first project where im trying do delete files from sd card incase it becomes full.
when i try to use an openNext arduino example everything is cool and working, however if i try to implement a remove file capeability with " sd.remove", i get unexpected output/results. i have been stuck with this bug for 5 days please your help will be appreceiated.

#include <SPI.h>

#include "SdFat.h"

// SD default chip select pin.
const uint8_t chipSelect =4;

// file system object
SdFat sd;

SdFile root;
SdFile file;

void setup() {
  Serial.begin(9600);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }
  
  Serial.println("Type any character to start");
  while (!Serial.available()) {
    SysCall::yield();
  }

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
    sd.initErrorHalt();
  }
  if (!root.open("/")) {
    sd.errorHalt("open root failed");
  }
  // Open next file in root.
  // Warning, openNext starts at the current directory position
  // so a rewind of the directory may be required.
  while (file.openNext(&root, O_RDONLY)) {
    char filename;
    file.getName(&filename,20);
    
    if (file.isDir()) {
      // Indicate a directory.
      Serial.print(&filename);
      Serial.println(" :directory detected");
    }else{
      
      Serial.println(&filename);
      if(sd.remove(&filename)){
        
           Serial.println(" :removed");
        }else{
          
          Serial.println(" failed to remove");
          
          }
      
      }
    Serial.println();
    file.close();
  }
  if (root.getError()) {
    Serial.println("openNext failed");
  } else {
    Serial.println("Done!");
  }
}
void loop() {}

Here is the output in serial monitor if you enter charactors four times,

Type any character to start
Type any character to start
Type any character to start
Type any character to start

Are you sure there are files on your SD card?

Have you tried just showing the file names, without removing the files?

Maybe it is necessary to re-open the root directory or .rewindDirectory() to use openNext() after removing a file.

You create a variable called filename that takes 1 char. You then try to stuff up to 20 chars into that variable. Did you mean to use

char filename[20];

also, these

 Serial.println(&filename);
      if(sd.remove(&filename)){
      ...

are more commonly written as

 Serial.println(filename);
      if(sd.remove(filename)){

when your variable is an array (e.g. filename[20]), the name alone is a pointer.

Yes please, if i do remove the if statement with sd.remove, everything is fine if i just serial print the &filename, the issue/bug comes when i add the if statement with " sd.remove()".
the card is filled with files, im assured of that please.

Thanks for your reply please,
What im trying to archieve is,to loop through each file on sd card and store file name in filename variable every iteration.
That means filename variable has to hold only one file name on each iteration as i delete the file.

No, everything is NOT fine. You are overrunning your variable. You can not stuff a filename into a 1 char length variable. You are just getting LUCKY that the program is not crashing.

Thanks again for the reply, but if write the while loop as below everything is fine,

while (file.openNext(&root, O_RDONLY)) {
    char filename;
    file.getName(&filename,20);
    
    if (file.isDir()) {
      // Indicate a directory.
      Serial.print(&filename);
      Serial.println(" :directory detected");
    }else{
      
      Serial.println(&filename);
          
      
      }
    Serial.println();
    file.close();
  }
  if (root.getError()) {
    Serial.println("openNext failed");
  } else {
    Serial.println("Done!");
  }

and here is the output in serial monitor;

Type any character to start
System Volume Infor :directory detected

20180201.CSV

20180601.CSV

20180701.CSV

20180801.CSV

20190101.CSV

20190501.CSV

20190701.CSV

except when i add sd.remove(&filename), thats whwn thigs become tough to me

No, it is NOT fine. Please stop saying that. You are just getting LUCKY since you are using memory you do not own. I have pointed out the issue, why do you not want to correct it?

while (file.openNext(&root, O_RDONLY)) {
    char filename[20];
    file.getName(filename,20);
    
    if (file.isDir()) {
      // Indicate a directory.
      Serial.print(filename);
      Serial.println(" :directory detected");
    }else{
      Serial.println(filename);
      }
    Serial.println();
    file.close();
  }
  if (root.getError()) {
    Serial.println("openNext failed");
  } else {
    Serial.println("Done!");
  }

Thanks once again, im gonna try revise on how to deal with pointers.
Any recommendations from you will be highly appreciated please.

Arduinos use C++ which includes a lot of C. There are tons of references and tutorials for getting started with C/C++.