SD card not working anymore when removing card

Hello friends.

What i am using: ESP32, Nextion display, SD card reader

I think the threadtitel says it all. I am using a SD card to store some measured data. Everything works fine. When i upload my scetch everything i want to log and store into my sd card works.
But when i unplugg the sd card and plug it back in, the sd card isnt working anymore.

What i have found out so far: From my research here in this forum assume that this part of code SD.begin(PIN);, is the key to my solution. So it will initalize the Sd card in my code once. Now when i unplugg and replug my Sd card back into my ESP, this initialization and connection is lost.

I also found a thread with the same problem. He solved the problem, with changing some stuff in SD.h and SD.cpp file. Since this is not my first choice, i want to ask ou guys if anyone found another solution. Plugging/unplugging SD card [solved] - Programming Questions - Arduino Forum

the code (part of it)

// put your setup code here, to run once:
void setup() {
  RTC_DS3231 rtc2;
  DateTime now3 = rtc2.now(); 
  nexInit();
  Serial.begin(9600);
  Serial2.begin(19200, SERIAL_8N1, RXD2, TXD2);
  EEPROM.begin(64);                                                                    //Nach derzeitgen Wissen definiert man so die Größe des EEPROMs. Man "reserviert" sich quasi Speicher. Müsste bis 255 gehen
  ph.begin();                                                                          //holt bzw. initialisiert sich die Daten der gespeicherten Messwerte für 7er und 4er Lösung aus EEPROM


  //Serial COM
  Wire.begin();
   // while the serial stream is not open, do nothing:
  while (!Serial) ;
  /***********************************************
  ****************SD Initializing process**********
  *************************************************/
  SD.begin(5);
  Serial.print("Initializing SD card...");
  if (!SD.begin(5))
  {
    Serial.println("initialization failed!");
    //return;
  }
  uint8_t cardType = SD.cardType();
  if(cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    //return;
  }
}

This is the part of the code, where i store the data on my Sd card. I also tried to execute the initialisation again in my function. Without sucsess.

void logSDCard_Alarms(String t_message)
{
  String fileName = "/Errormessages.txt";
  char charFileName[fileName.length() + 1];
  fileName.toCharArray(charFileName, sizeof(charFileName));
  String dataMessage;
  uint8_t cardType = SD.cardType();
  
  SD.begin(5);    //test

  File dataFile = SD.open(charFileName);
    if(!dataFile) 
    {
      Serial.println("File doens't exist");
      Serial.println("Creating file...");
      writeFile(SD, charFileName, "Timestamp,Message \r\n");
    }
  
  dataMessage =  t_message + "\r\n";
  Serial.print("Save data: ");
  Serial.println(dataMessage);
  appendFile(SD, charFileName, dataMessage.c_str());
  dataFile.close();
}

Any ideas?

Thank you

So i think that the problem is, that you can only use SD.begin(PIN) once. Has anyone encountered the same problem?

thank you