In SPIFFS, How can i deleted old data for adding new data?

I have ESP8266 module, the module is connected with a temperature sensor and the values are store in SPIFFS.
i planned to erase the old data one by one with new data when SPIIFS memory is full.
could you please help me to code that way?
this is the portion of code i am tied

void write_task(char *cFileName)
{
 int c=0;
int s=0;
int record_no=0;
  
  bool ok = SPIFFS.begin(true);
  if (!ok)
  {
#ifdef DEBUG
    Serial.println("File not begin");
#endif
  }
  else
  {
    iHdl = SPIFFS.open(cFileName, "r+");
    if (!iHdl)
    {
#ifdef DEBUG
      Serial.println("File not opened");
#endif
    }
    else
    {
    if(c!=0)
    {
      if(c>=3)
       {
        record_no=c-4;
       }
       
       else
       record_no=c;
         task_edit(record_no);
       
    }
    else
    {
     
    iHdl.write((uint8_t *)&obj_task, sizeof(obj_task));
    iHdl.close();
    }
    c++;

       
      Serial.println("File SUCESS");
       delay(100);
        iHdl = SPIFFS.open(cFileName, "r");
           int c = iHdl.size() / sizeof(obj_task);
        for(int i=0;i<c;i++)
        {
       iHdl.read((uint8_t *)&obj_task, sizeof(obj_task));
       Serial.println(obj_task.Date);
        }
       iHdl.close();
    }
  }
  
  
  void  task_edit(int record)
{
struct TASK b; 

iHdl.seek(record * sizeof(struct TASK));
iHdl.read( ( uint8_t*)&b, sizeof(struct TASK));
b=obj_task;
iHdl.seek(record * sizeof(struct TASK));
iHdl.write( (uint8_t*)&b, sizeof(struct TASK));

    iHdl.close();
}

SPIFFS.remove() possibly ?

How will you know which one to remove ?

removing method is fist in fist out..
which is happens automatically

Are you sure about that ?

The references that I have found seem to indicate that it requires a filename

ESP8266 Core

sir actually that's my requirement(fist in fist out)

OK, then you will need to implement it yourself and keep a list of files in some kind of FIFO structure, such as an array, or maybe make the filenames sequential numbers or dates/times so that you can search for and delete the earliest one

Will all of the files be of the same size ? If not then there is a danger of deleting a small one and replacing it with a larger one that there is not space for

How will you recognise that you are running out of space ?

I have only a single file, the data in the file is the matter.

assuming each temperature record (say 2 bytes) is timestamped with 4 bytes in your spiff file = 6 bytes
assuming you record t° every 10 minutes (6 per hour).
assuming you configured your ESP8086 4 Megs storage as 1 megabyte for the program and 3 for the spiff you can record more than 524k samples - roughly 10 years worth of samples...

whilst it's good to think about space usage, do you think it will be necessary ?
(if you store in ASCII of course duration will be shorter)

but ..when the size of data increases or SPIFFS size limited, how to solve this issue..?

don't use SPIFFS, use an SD Card, you'll get Gigs of storage instead of Megs and the limit is really far away :slight_smile:

if you want to shrink a file, that won't do, file system does not let you do so. You would need to duplicate from one point to the end in another file but as you are short in memory, that will be a challenge (or need to do so when you reach half the space)

easier option is to not use just one file, create one per month and when storage starts to be full, get rid of the oldest month files.