VBA Delete File from Computer Folder

There should be multiple ways to do it. In this article, I have given 2 methods.

One is by using Filesystemobject & other uses VBA.Kill function.

Parameter for both is the same. The file path or wildcard.

Here is the sample VBA code to Delete a file:

VBA Code to Delete File:

Important Note: File once deleted with this option cannot be recovered & it cannot be found in Recycle bin.

This option is like deleting the file with Shift + Delete. So, be very careful while testing this option with any file.

Sub VBA_Delete_File()
    Dim fso As Object, filePath As String
    
    Set fso = Create.Object("Scripting.Filesystemobject")
    filePath = "D:\SomeFilename.txt"
    
    fso.deletefile (filePath) 'OR
    VBA.Kill (filePath)
    
    MsgBox "Files Deleted"
    
End Sub

Permission Denied while Delete File Operation

This is a common error that occurs many times. To this, follow these options.

  1. Check if the file exists.
  2. Verify if the folder path is accurate.
  3. Does folder has enough access rights enabled for deleting files.
  4. Check that the folder you are processing does not have multiple spaces in its name or try hanging the folder name to “Folder_For_Processing” or “FolderForProcessing”.

Once you check both the above options the functions will work just fine.