I have a datafile, datalog.txt I can open, read and write cool. What is the best way to delete all the file contents? Should I try rewriting the SD card file with NULL data? I have been reading the "SD.h" header file. I see a flush() and truncate() function should I use one of these?

or maybe I should just close and reopen the file, and overwrite the data. But I'm concerned that this might lead to some miss representations if the old data is not completely removed.

I am using a teensy 4.1 with built_in SD card.

  • Just open the file. There is no "not completely removed".
    – Majenko
    Commented Nov 12, 2021 at 17:07

1 Answer 1


To remove a file use SD.remove("datalog.txt");

SD library's FILE_WRITE is O_READ | O_WRITE | O_CREAT | O_APPEND so it would append at the end of the existing file.

to start with an empty file: File file = FS.open("datalog.txt", O_READ | O_WRITE | O_CREAT);

EDIT: opening the file with O_READ | O_WRITE | O_CREAT doesn't clear the content of the file, only starts to overwrite it from the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.