Excel VBA Delete a Workbook: Safe Methods and Code

VBA & macros · Updated November 3, 2026

Excel VBA Delete a Workbook: Safe Methods and Code

Yes. Excel VBA can delete a workbook file from disk, but the workbook must be closed first: Workbook.Close closes it, while VBA’s Kill statement deletes the file. To reduce the risk of losing work or removing the wrong file, run the macro from a separate workbook, check the full file path, and confirm the deletion.

Closing a workbook is not the same as deleting it

Excel uses “close” and “delete” for two different actions. Calling a workbook’s Close method removes it from the current Excel session. The file remains in its folder and can be opened again. Deleting removes the file from storage, so it is no longer available at that path.

This distinction matters because Excel normally keeps an open workbook file in use. A macro can close a target workbook and then use Kill on its path. Calling Kill while the file is still open can fail with a permission or file-access error. Closing a workbook from VBA alone does not erase it.

Deleting a workbook file is also different from deleting a worksheet. A worksheet is an object inside a workbook; a workbook is a file such as an .xlsx or .xlsm file. For a sheet, VBA uses a worksheet’s Delete method, which removes that sheet from the workbook. For a workbook file, the relevant sequence is close the workbook, then delete its file.

A safe VBA example for a Windows file

The example below expects an ordinary, existing workbook at the specified absolute Windows path. It checks that the file exists, refuses to delete the workbook that contains the macro, asks for confirmation, closes a matching open workbook without saving changes, and then attempts to delete the file. Change the example path to the exact workbook you intend to remove.

Save a separate copy of any important work before running a macro. Deletion cannot be undone with Ctrl+Z, and Kill does not send the file to the Recycle Bin.

Public Sub DeleteTargetWorkbook()
    Dim targetPath As String
    Dim targetWb As Workbook
    Dim isOpen As Boolean
    Dim answer As VbMsgBoxResult

    On Error GoTo DeleteError

    ' Replace this with the target workbook's full path.
    targetPath = "C:\Reports\OldReport.xlsx"

    ' Stop if the path does not identify an existing file.
    If Len(Dir$(targetPath, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)) = 0 Then
        MsgBox "The workbook file was not found.", vbExclamation
        Exit Sub
    End If

    ' Find the target in this Excel instance and protect the macro host.
    For Each targetWb In Application.Workbooks
        If StrComp(targetWb.FullName, targetPath, vbTextCompare) = 0 Then
            If targetWb Is ThisWorkbook Then
                MsgBox "Run this macro from a different workbook.", vbExclamation
                Exit Sub
            End If
            isOpen = True
            Exit For
        End If
    Next targetWb

    answer = MsgBox( _
        "Permanently delete this workbook? If it is open, unsaved changes will be discarded:" & _
        vbCrLf & targetPath, vbYesNo Or vbQuestion, "Confirm deletion")
    If answer <> vbYes Then Exit Sub

    ' Close the target first; do not save pending changes.
    If isOpen Then targetWb.Close SaveChanges:=False

    ' Kill deletes the file; it does not move it to the Recycle Bin.
    Kill targetPath
    MsgBox "The workbook file was deleted.", vbInformation
    Exit Sub

DeleteError:
    MsgBox "The workbook could not be deleted: " & Err.Description, vbExclamation
End Sub

The prompt is important: the example deliberately uses SaveChanges:=False, so any unsaved edits in the target are discarded when it closes. If you need those edits, save them first or change the workflow so that the workbook is saved before it is closed. A deletion error is reported rather than silently ignored.

Where to put the macro and how to run it

Because the target must be closed before its file can be deleted, a macro stored inside that same target workbook is a poor place to run this procedure. The example detects that case and stops. Put the code in another macro-enabled workbook, in Personal.xlsb, or in an add-in. Personal.xlsb is a hidden workbook Excel can load when it starts; an add-in can also provide reusable VBA.

  1. In desktop Excel for Windows, open the VBA editor with Alt+F11. On a Mac, Option+F11 is the common shortcut; some keyboards require Fn as well.
  2. In the editor, choose Insert > Module, paste the code, and replace the sample path with the target’s full path.
  3. Save the controller workbook as an Excel Macro-Enabled Workbook (.xlsm) if storing the macro there. Run the macro from Excel, then read the confirmation carefully.

Excel for the web does not run VBA macros; use desktop Excel. Excel for Mac supports VBA, but this code’s C:\Reports\... path is a Windows path, not a Mac path. A Mac user must use a path appropriate to macOS and account for that system’s file-access permissions. Do not assume a Windows path can simply be pasted into Excel for Mac.

Choosing the correct file path

Most accidental deletions come from a path that points somewhere unexpected. Use the full path, including the drive, folders, filename, and extension—for example, C:\Reports\OldReport.xlsx. A relative filename such as OldReport.xlsx can depend on the current working directory, which may not be the folder you expect.

  • Check the spelling, extension, and folder in File Explorer before running the macro.
  • Do not build the target path from an unverified cell or user entry. Validate that value before using it in a deletion routine.
  • If the workbook is stored on a network share or synced folder, confirm the actual location and that you have permission to delete it.
  • A matching workbook open in another Excel process may not appear in the current instance’s Application.Workbooks collection. The deletion may fail while that other process holds the file.

The sample compares full paths without regard to letter case, which is suitable for typical Windows paths. If your workflow accepts paths from different sources, differences such as mapped drives versus network paths can still make two references to the same location look different. Use a single, known path format rather than assuming every equivalent-looking path will compare identically.

What Kill does—and what it does not do

VBA’s Kill statement deletes a file identified by its path. It is not an Excel workbook method and does not first close the workbook for you. It does not provide a recycle-bin recovery step. Treat it as a permanent file operation and make a backup if there is any chance you will need the workbook later.

Although Kill can accept wildcard characters, avoid wildcards in a routine intended to remove one workbook. A pattern such as C:\Reports\*.xlsx can match many files, not just the one you had in mind. This example uses a specific filename and requires confirmation. If you genuinely need batch deletion, first list and inspect every matching path, then require a separate, deliberate confirmation before removing anything.

Deleting a file also does not guarantee that every copy is gone. A synced service, backup system, email attachment, or another folder may contain a separate copy. The macro acts on the one path it receives; it does not search for duplicates or manage cloud retention.

Common errors and how to diagnose them

If the code reports an error, check the cause rather than adding a broad error-suppression statement. Hiding errors can make a failed deletion look successful or conceal that a different file was affected.

  • File not found: Check the full path, extension, spelling, and whether the file was moved or renamed. Also verify that the path is valid in the Windows account running Excel.
  • Permission denied or access error: The file may still be open, read-only, in use by another process, or in a folder where your account cannot delete files. Close other Excel instances and check folder permissions.
  • Workbook open elsewhere: The macro searches only workbooks in its current Excel instance. Close the target in any other Excel process and try again.
  • Network or sync conflict: A disconnected share or synchronization lock can prevent deletion. Confirm access to the location before rerunning the macro.
  • Unexpected deletion: Stop using the macro, check the exact path in the code, and restore from a backup if available. Ctrl+Z does not reverse a VBA file deletion.

The sample’s error handler displays the error description, but it cannot determine the exact cause in every environment. For important files, test the workflow on a disposable copy in a temporary folder before using it on a real workbook.

Alternatives when VBA is not the right tool

If this is a one-time cleanup, close the workbook and delete it in File Explorer instead. That makes the selected file visible and can allow recovery from the Recycle Bin, depending on the location and system configuration. For a file in OneDrive or another managed service, use the service’s normal deletion and recovery process.

If the goal is only to stop Excel from showing the workbook, close it rather than delete it. If the goal is to remove a worksheet, use the worksheet-level operation and confirm that the remaining workbook is saved. If the goal is to clean a set of old files, consider moving them to an archive folder first; that gives you a review period before permanent removal.

Use VBA deletion when the process is repeatable, the target path is controlled, and the confirmation and backup policy are clear. Avoid automating deletion based only on a filename prefix, a changing cell value, or a broad wildcard unless the routine first shows exactly which files it plans to remove.

Frequently Asked Questions

Can VBA delete the workbook that contains the macro?
It is not a reliable approach to run deletion code from the workbook being deleted. The code host must remain available while the procedure runs, so put the macro in a separate controller workbook, Personal.xlsb, or an add-in. The example stops if the target is ThisWorkbook.

Does Workbook.Close delete the workbook file?
No. Workbook.Close closes the workbook in Excel but leaves its file in the folder. To delete the file, close it first and then use a file-deletion operation such as Kill.

Can I undo a VBA deletion with Ctrl+Z?
No. Excel’s undo history does not reverse file-system actions performed by a macro. Make a backup beforehand, or use a deletion method with a recovery option when you need one.

Will this macro work in Excel for the web?
No. Excel for the web does not execute VBA. Run the macro in desktop Excel, and change the path approach for Mac because the Windows path in the example is not valid as written on macOS.

Why does Kill fail even though the path is correct?
The workbook may still be open in another Excel instance, the file may be read-only or in use, or your account may lack permission to delete it. Close other processes using the file and check its attributes, location, and folder permissions.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.