VBA Kill function

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)


The VBA Kill function deletes a file based on the provided pathname.

If using Windows you can also use the FileSystemObject VBA DeleteFile method which allows you to delete a file or folder.


The syntax for the Kill function is:


Parameter Variable Type Description
pathname String The pathname of the file you want to delete or a pathname including wildcards.

Other Notes

The Kill function’s pathname parameter can include wildcards. You can use wildcard characters to specify multiple files to be deleted. The patterns that you can choose from are:

Wildcard Description
* Allows you to match any string of any length (including zero length)
? Allows you to match on a single character

The VBA Kill function will not delete readonly files.

Example usage

The Kill function can be used in VBA. Let’s look at some Kill function examples:

As mentioned above the VBA Kill function will not delete readonly files. Therefore to delete a file in VBA you need to make sure its file property is set to vbNormal (use SetAttr function). Similarly it is good to verify if the file exists before attempting to delete it. The function below takes care of both:

Recursive file deleting

In some cases you might want to delete all files of a certain filename in the directory and all subsequent directories. In this case in my post Deleting files using VBA I explored a recursive file delete function:

Delete all copies of file “1.txt” in the given and below directories like this:

Simply the best place to learn Excel VBA