Yes. In Excel VBA for Microsoft 365 on Windows, the Kill statement can delete a workbook file by its full path, provided the file is closed and you have permission to remove it. Save a separate copy before running deletion code: deleting a file this way is not an action you can undo with Ctrl+Z.
What “delete a workbook” means
A workbook is the Excel file itself, such as MonthlyReport.xlsx; it can contain several worksheets. Deleting a workbook file removes that file from its folder. Deleting a worksheet removes one tab from a workbook but leaves the workbook file in place. The two operations use different VBA approaches, so confirm which result you want before running a macro.
The example below deletes a different, closed workbook from disk. It does not delete the workbook that contains the macro, and it does not remove a worksheet. That distinction matters because a workbook cannot normally be removed with Kill while Excel still has it open. A macro that closes its own workbook also cannot continue running code from that workbook after it has closed.
How VBA deletes a workbook file
Kill is a built-in VBA statement that removes a file at the path you provide. Use a complete path, including the drive or network location, folder names, and file extension. For example, a path might look like C:\Reports\Archive\OldReport.xlsx. The folder must exist, and the account running Excel must have permission to delete the file.
Unlike deleting a file in File Explorer, Kill does not send the file to the Windows Recycle Bin. Treat it as a permanent deletion. A cloud-sync service or network backup may keep its own deleted-file recovery or version history, but that depends on the service and its settings; it is not a substitute for a backup you control.
The statement is deliberately simple, which makes validation important. A mistaken path can remove the wrong file, and a wildcard path can match multiple files. The sample uses a single explicit filename, checks that the path is not the macro workbook, checks whether the file exists, and asks for confirmation before deleting it.
A safer VBA example
Before running any macro that deletes a file, save a separate copy of important workbooks. Macros cannot be undone with Ctrl+Z. Put this code in a trusted macro-enabled controller workbook, then change targetPath to the exact path of the closed workbook you intend to remove.
Sub DeleteClosedWorkbookFile()
Dim targetPath As String
Dim answer As VbMsgBoxResult
On Error GoTo DeleteFailed
' Enter the exact path of the workbook to remove.
targetPath = "C:\Reports\Archive\OldReport.xlsx"
' Do not let this macro remove the workbook containing the code.
If StrComp(targetPath, ThisWorkbook.FullName, vbTextCompare) = 0 Then
MsgBox "Choose a different workbook file.", vbExclamation
Exit Sub
End If
' Stop if the specified file cannot be found.
If Len(Dir$(targetPath)) = 0 Then
MsgBox "The specified file was not found.", vbInformation
Exit Sub
End If
' Require an explicit confirmation before permanent deletion.
answer = MsgBox("Permanently delete this file?" & vbCrLf & _
targetPath, vbYesNo + vbExclamation, "Delete workbook")
If answer <> vbYes Then Exit Sub
Kill targetPath
MsgBox "The workbook file was deleted.", vbInformation
Exit Sub
DeleteFailed:
MsgBox "The file could not be deleted: " & Err.Description, vbExclamation
End Sub
The error handler displays the reason Excel reports if deletion fails. It does not fix the cause or retry automatically; inspect the message and check the file, folder, and permissions before trying again. Keep the confirmation prompt in place unless you have a specific, controlled reason to remove it.
Run the macro and verify the target
In desktop Excel for Windows, first save the controller workbook as an Excel Macro-Enabled Workbook (.xlsm) if it contains VBA. Open the Visual Basic Editor with Alt+F11, insert the procedure in a standard module, and replace the example path. You can run the procedure from the editor with F5, or return to Excel, open Developer > Macros, select DeleteClosedWorkbookFile, and choose Run. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.
On a Mac, Excel also supports VBA, but the keyboard shortcut to open the Visual Basic Editor can depend on the keyboard and function-key settings. Use Excel’s Tools > Macro menu to open the editor or run a macro if the shortcut differs. Excel for the web cannot create, edit, or run VBA macros; open the file in the desktop app instead. Macro security settings may also block code until you choose an appropriate trusted location or enable macros for a file you trust. Do not enable macros in an unfamiliar workbook just to delete a file.
Before you run the procedure, check the path character by character, including the filename and extension. A workbook saved as .xlsm is not the same path as one ending in .xlsx. If the macro says the file was not found, confirm that the drive or network location is available and that the path is spelled correctly. After a successful message, check the folder or its recovery options if you need to confirm what happened.
Open workbooks, unsaved changes, and deleting the macro file
Close the target workbook before running the sample. If it is open in the same Excel session, Windows may prevent the deletion because the file is in use. A workbook open in another Excel process, or a file held by another application, can cause a similar failure. The example does not close open workbooks automatically because closing one could discard unsaved edits or interrupt someone’s work.
If you need to delete a workbook that is currently open, decide first what should happen to its changes. Save or discard them deliberately, close the workbook, and then run deletion from a separate controller workbook. If Excel reports that the file is still in use, close any other session or application using it and try again; do not assume that repeatedly running Kill will release the file.
Deleting the workbook that contains the running macro is a different problem. The code is executing from that file, so it cannot safely continue after closing the file. Use a separate controller workbook or another process to perform the deletion after the target closes. The sample’s ThisWorkbook check blocks deletion when the entered path exactly identifies the controller workbook, but it is still your responsibility to confirm that the path points to the intended file.
Common reasons deletion fails
A failed Kill call usually points to a file-state, path, or permission issue rather than a problem with the workbook’s contents. Check these causes before changing the code:
- The workbook is open or in use. Close it in Excel and check whether another Excel process, preview pane, backup tool, or application is holding the file.
- The path is wrong or unavailable. Check the drive letter, folder names, extension, and network connection. A mapped drive may not be available in the context where Excel is running.
- You lack delete permission. Folder permissions, organizational policies, or network sharing rules can prevent deletion even when you can open the file.
- The file is read-only. VBA’s
Killstatement cannot delete a read-only file. Do not automatically clear attributes without understanding why they are set and confirming you are allowed to change them. - A sync or backup provider is involved. A provider may delay, reject, or later synchronize a deletion. Check its status and recovery options rather than assuming the local result describes every copy.
The error message can help distinguish these situations, but it may be brief or generic. Avoid suppressing errors with On Error Resume Next around the deletion unless you also check and report the error; otherwise, a macro can appear to succeed when the file remains.
Deleting a worksheet instead of a workbook
If you meant to remove one sheet tab, use the worksheet collection rather than Kill. In Windows Excel, the interface route is Home > Delete > Delete Sheet after selecting the sheet. VBA can remove a named sheet, but Excel normally asks for confirmation because sheet deletion cannot be undone with Ctrl+Z. Disabling alerts can bypass that confirmation, so any code that does so should first validate the workbook and sheet name and should restore the previous alert setting afterward.
Deleting a worksheet can permanently remove its cells, formulas, charts, and other sheet-level content. It does not delete the workbook file, and other sheets remain. Conversely, deleting a workbook file removes the whole file, including every sheet it contains. When a reader says “delete workbook,” asking whether they mean the file or a tab is often the most important safety check.
Safer alternatives and good safeguards
If the aim is to clean up old files, consider moving them to a review folder first instead of immediately deleting them. A person can inspect the files and remove them later through File Explorer, where deletion may use the Recycle Bin. For a one-off cleanup, manual deletion may be safer than writing a macro, especially when there are only a few files or the correct target is uncertain.
For routine automation, use a separate controller workbook, explicit full paths, a confirmation or review step, and a log that records what the macro attempted. Avoid broad wildcards and folders whose contents change unpredictably. Test the procedure against a disposable sample file before using it on important data, and make sure that any backup or retention requirements are met. A macro is most useful when the target and deletion rules are stable and clearly defined—not as a shortcut around checking what a file contains.
Frequently Asked Questions
Can VBA delete an Excel workbook that is open?
Usually not while Excel or another process has the file open. Close it first, handling unsaved changes deliberately, then run deletion from a separate controller workbook. The sample reports a failure rather than closing the target automatically.
Does VBA’s Kill command send a workbook to the Recycle Bin?
No. Kill removes the file without sending it to the Windows Recycle Bin. Recovery may still be possible through a backup or a cloud service’s recycle or version-history feature, depending on its configuration, but do not rely on that.
Can a workbook delete itself with VBA?
A macro cannot reliably keep running from a workbook after that workbook closes. Use a separate controller workbook or another process to close and then delete the target. Keep a backup if the file contains anything you may need.
Why does VBA say it cannot delete the file?
Common causes include an open or locked file, an incorrect path, missing folder permissions, an unavailable network drive, or a read-only attribute. Check the exact path and file state, then address the relevant access issue rather than suppressing the error.
How do I delete a worksheet rather than the workbook file?
Select the worksheet and use Home > Delete > Delete Sheet, or use VBA’s worksheet deletion methods. That removes a sheet from the open workbook; it does not delete the workbook file from its folder.
Related
- Microsoft Excel Scatter Plot: How to Create and Read One
- Microsoft Excel Power Query: What It Does and How to Use It
- Microsoft Excel Print Area: Set, Change, and Clear
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Delete a Specific Row With a VBA MacroYes. In Excel for Microsoft 365 on Windows, a VBA macro can delete a specific worksheet row with…
- Excel VBA Exit Sub: How It Works and When to Use ItIn Excel VBA, Exit Sub immediately ends the currently running Sub procedure and returns control to the code…
- Access VBA String to Number: Safe Conversion GuideIn Access VBA, convert a numeric string with the function that matches the number you need: use CLng…
- Excel VBA: How to Enable AutoFilterTo enable AutoFilter with VBA, select the worksheet range that includes the column headers and run its AutoFilter…
- VBA Range.Insert: Insert Cells, Rows, and Columns in ExcelIn VBA, use the Range.Insert method to add cells, rows, or columns and shift existing content out of…
- Excel VBA UserForm Data Entry: Build a Simple FormTo enter data through an Excel VBA UserForm, create a form with text boxes and a Save button,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.