Delete a file specified by its file name SysUtils unit
 function DeleteFile ( const FileName : string ) : Boolean;
The DeleteFile function deletes a file given by its file name FileName.
The file is looked for in the current directory.
If the file was deleted OK, then True is returned, otherwise False is returned.
This function is easier to use than the equivalent System unit Erase routine.
Warning : the Windows unit also has a DeleteFile function that takes a PChar argument.

To ensure that you use are using the intended one, type SysUtils.DeleteFile.
Related commands
AssignFile Assigns a file handle to a binary or text file
CloseFile Closes an open file
Erase Erase a file
Rename Rename a file
RenameFile Rename a file or directory
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : Try to delete a file twice
  fileName : string;
  myFile   : TextFile;
  data     : string;

  // Try to open a text file for writing to
  fileName := 'Test.txt';
  AssignFile(myFile, fileName);

  // Write to the file
  Write(myFile, 'Hello World');

  // Close the file

  // Reopen the file in read mode

  // Display the file contents
  while not Eof(myFile) do
    ReadLn(myFile, data);

  // Close the file for the last time

  // Now delete the file
  if DeleteFile(fileName)
  then ShowMessage(fileName+' deleted OK')
  else ShowMessage(fileName+' not deleted');

  // Try to delete the file again
  if DeleteFile(fileName)
  then ShowMessage(fileName+' deleted OK again!')
  else ShowMessage(fileName+' not deleted, error = '+
Show full unit code
   Hello World
   Test.txt deleted OK
   Test.txt not deleted, error = 2
Delphi Programming Neil Moffatt 2002 - 2016. All rights reserved.  |  Home Page