ZetCode

FreeBasic Kill Keyword

Last modified October 18, 2026

The FreeBasic Kill keyword is used to delete files from the filesystem. It provides a simple way to remove files programmatically. This command is powerful and should be used with caution.

Basic Definition

The Kill statement in FreeBasic permanently deletes a file specified by a path. Once deleted, files cannot be recovered. In FreeBasic Kill can also be used as a function that returns zero on success or a non-zero error code on failure.

The Kill statement follows the syntax: Kill "filepath". The filepath can be absolute or relative. If the file does not exist or cannot be deleted, Kill does not stop the program; it returns a non-zero error code (for a missing file, 2). To detect failure, use the function form and check its return value.

Deleting a Single File

This example demonstrates how to delete a single file using the Kill statement.

kill_single.bas
' Create a temporary file for demonstration
Open "tempfile.txt" For Output As #1
Print #1, "This is a temporary file"
Close #1

Print "File exists before deletion: "; Len(Dir("tempfile.txt")) > 0
Kill "tempfile.txt"
Print "File exists after deletion: "; Len(Dir("tempfile.txt")) > 0

First, we create a temporary file and verify its existence. Then we use Kill to delete it. The Dir function returns the file name if it exists, or an empty string otherwise, so comparing its length with zero checks existence before and after deletion. Running this prints -1 (true) before deletion and 0 (false) after.

Deleting Multiple Files with Wildcards

The Dir function accepts wildcards to list matching files, but Kill does not expand them: Kill "temp_*.txt" does not delete multiple files on the test build (Linux, fbc 1.10.1), and returns a non-zero error code. Delete matching files one at a time.

kill_wildcards.bas
' Create multiple temporary files
For i As Integer = 1 To 5
    Open "temp_" & i & ".txt" For Output As #1
    Print #1, "File number "; i
    Close #1
Next

Print "Files created: "; Dir("temp_*.txt")
Dim r As Long
For i As Integer = 1 To 5
    r = Kill("temp_" & i & ".txt")
Next
Print "Files after deletion: "; Dir("temp_*.txt")

This code creates five temporary files with numbered names. Each file is deleted with its own Kill call. The Dir function lists the first matching file before deletion and returns an empty string after all five have been removed.

Error Handling with Kill

Kill reports failure through its return value, so error handling means checking that value.

kill_error.bas
Dim r As Long

' Try to delete a non-existent file
r = Kill("nonexistent.txt")
If r <> 0 Then
    Print "Kill failed, error code "; r
Else
    Print "File deleted"
End If
Print "This line is reached"

This example shows how to handle failures when using Kill. We attempt to delete a non-existent file. Kill does not raise a runtime error here; it returns 2, which the program checks and reports. The program then continues normally.

Deleting Files in a Different Directory

Kill can delete files in any directory with proper path specification.

kill_directory.bas
' Create a file in a subdirectory
MkDir "tempdir"
Open "tempdir/testfile.txt" For Output As #1
Print #1, "Test content"
Close #1

Print "File in directory exists: "; Len(Dir("tempdir/testfile.txt")) > 0
Kill "tempdir/testfile.txt"
Print "File after deletion: "; Len(Dir("tempdir/testfile.txt")) > 0

' Clean up directory
RmDir "tempdir"

Here we create a subdirectory and a file within it. We then delete the file using a relative path. Finally, we remove the empty directory with RmDir, since Kill is meant for files. Note that the platform behavior of Kill on directories is not defined by the FreeBasic documentation; on Linux, Kill on an empty directory succeeds and removes it, so use RmDir for directories to get portable behavior.

Checking File Existence Before Deletion

It is good practice to check if a file exists before attempting to delete it.

kill_check.bas
Dim filename As String = "checkfile.txt"

' Create file only if it doesn't exist
If Len(Dir(filename)) = 0 Then
    Open filename For Output As #1
    Print #1, "Sample content"
    Close #1
End If

' Safe deletion
If Len(Dir(filename)) > 0 Then
    If Kill(filename) = 0 Then Print "File deleted successfully"
Else
    Print "File doesn't exist"
End If

This example demonstrates defensive programming with Kill. We first check if the file exists before creating it. Then we check again before deletion. This prevents errors and makes the code more robust.

Deleting Read-Only Files

Kill has no portable read-only handling in FreeBasic. The standard runtime does not provide a SetAttr or FB_READONLY to change file attributes, so the check must rely on the return value of Kill.

kill_readonly.bas
' Create a file
Open "readonly.txt" For Output As #1
Print #1, "Read-only content"
Close #1

Dim r As Long = Kill("readonly.txt")
If r = 0 Then
    Print "File deleted successfully"
Else
    Print "Error deleting file, code "; r
End If

This code creates a file and deletes it, checking the return value of Kill. A non-zero return indicates that deletion failed, for example because the file is locked or the permissions do not allow it.

Best Practices

This tutorial covered the FreeBasic Kill keyword with practical examples showing its usage in different scenarios. Use this powerful command responsibly.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.