When ESP32S3 is writing data to SD card, how to remove the card safely?

Thanks. I know this procedure but the problem is the "When finished read/write-ing" part. I think I was told to do the sampling as fast as possible so I guess I need to avoid the if-statement within the loop() function to test when, say a certain number of lines have been written or when a button/key is pressed. If I just unplug the card, I might corrup the file/file system or damage the sd card. Without using flush() to clear the buffer and then closing the file, the data file may not be written successfully.

Please refer to Post#31 and Post#47

How to obtain and save sensor data at specific samplying rate using Adalogger with RTC + SD? - Other Hardware / 3rd Party Boards - Arduino Forum