[Solved] How to delete a file lazarus / 21-General ---------------------------------------------------------------------- bobonwhidbey · 2022-06-10 17:10 Deleting a file has become a problem for me. This seems to be a new phenomena - perhaps a recent Windows update? This coding results in an endless loop: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     SomeFile := 'C:\Users\bob\Documents\Test.txt';    repeat      DeleteFile(SomeFile);      until not FileExists(SomeFile); ---------------------------------------------------------------------- Red_prig · 2022-06-10 21:14 Maybe DeleteFile returns false just because you don't have access rights? ---------------------------------------------------------------------- wp · 2022-06-10 21:26 The following code creates a test file (Button2) and deletes it (Button1, your code). It works correctly for me. But when I comment the "CloseFile" in Button2Click I have the infinite loop that you mention, because an open file cannot be deleted. So I guess you should check whether the file that you want to delete is not open by your or another program. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  fn: String;  n: Integer;begin  fn := 'test.dat';  n := 0;  repeat    DeleteFile(fn);  inc(n);    Caption := IntToStr(n) + ' attempts needed.';  until not FileExists(fn);end; procedure TForm1.Button2Click(Sender: TObject);var  F: TextFile;begin  AssignFile(F, 'test.dat');  Rewrite(F);  WriteLn(F, 'test test test');  CloseFile(F);end; ---------------------------------------------------------------------- bobonwhidbey · 2022-06-10 22:25 Thank you. The file WAS opened, and thus couldn't be deleted. ---------------------------------------------------------------------- marcov · 2022-06-11 10:05 wp: better have some pauze between deletions, and give up after a maximum. (like 5 times 100 or 250ms or so). See fpmkunit how the FPC "fpmake clean" works around these kinds of issues. ---------------------------------------------------------------------- wp · 2022-06-11 10:24 Quote from: marcov on June 11, 2022, 12:05:24 pmwp: better have some pauze between deletions, and give up after a maximum. (like 5 times 100 or 250ms or so). Of course, and normally the loop is not necessary at all. Just wanted to use the OP's code and point out that this issue can occur when a file is still open.