This guide shows how to delete all Power Query queries and data connections in an Excel workbook using VBA, which is useful for cleaning up unused connections, improving performance, or preparing a file for sharing.

What You'll Need

  • Microsoft Excel (2010 or later, including Excel 365)
  • A workbook containing queries and connections to delete
  • Basic access to the VBA editor (Alt+F11)

Step-by-Step

  1. Open the Excel workbook that contains the queries and connections you want to remove.
  2. Press Alt+F11 to open the VBA editor.
  3. In the VBA editor, click Insert > Module to create a new module if one does not already exist.
  4. Copy and paste the following VBA code into the module window:
    Sub DeleteAllQueriesAndConnections()
        Dim cn As WorkbookConnection
        Dim qt As QueryTable
        Dim ls As ListObject
        
        ' Delete all workbook connections (including Power Query, ODBC, etc.)
        For Each cn In ThisWorkbook.Connections
            On Error Resume Next
            cn.Delete
            On Error GoTo 0
        Next cn
        
        ' Delete all query tables (legacy connections)
        For Each qt In ThisWorkbook.Worksheets.QueryTables
            On Error Resume Next
            qt.Delete
            On Error GoTo 0
        Next qt
        
        ' Delete all ListObjects that are sourced from queries or connections
        For Each ls In ThisWorkbook.ListObjects
            On Error Resume Next
            If ls.SourceType = xlSrcExternal Or ls.SourceType = xlSrcQuery Then
                ls.Delete
            End If
            On Error GoTo 0
        Next ls
        
        ' Optional: Clear the workbook's connection cache (helps prevent ghost connections)
        ThisWorkbook.Connections.Item(1).Refresh ' Dummy refresh to trigger cleanup if any connections remain
        
        MsgBox "All queries and connections have been deleted.", vbInformation
    End Sub
  5. Close the VBA editor and return to Excel.
  6. Press Alt+F8, select DeleteAllQueriesAndConnections, and click Run.
  7. Wait for the macro to finish — a message box will confirm completion.
  8. Save the workbook (Ctrl+S) to preserve the changes.
  9. Common Mistakes

    • Running the macro without saving first: If the macro fails or behaves unexpectedly, you may lose unsaved changes. Always save a backup before running deletion code.
    • Assuming deleting queries removes the data: Deleting a query or connection only removes the link to the source data; any loaded data in worksheets remains unless manually cleared.
    • Overlooking ListObjects: Some queries load data into Excel Tables (ListObjects). The code above checks for externally sourced tables and deletes them — skipping this step leaves orphaned tables.
    • Using ThisWorkbook.Connections.Delete directly: This causes a runtime error because the collection changes during iteration. The code uses On Error Resume Next to safely handle each deletion.
    • Not checking for protected sheets or workbook structure: If the workbook is protected, you cannot delete connections. Unprotect it first via Review > Unprotect Workbook or Sheet.

    Tips

    • Backup your file: Before running any macro that deletes data or connections, save a copy of the workbook with a different name or in a backup folder.
    • Test on a copy first: If you're unsure, duplicate the workbook and run the macro on the copy to verify it behaves as expected.
    • Use the Immediate Window for single deletions: To delete one connection manually, go to the Immediate Window (Ctrl+G in VBA editor) and type: ThisWorkbook.Connections("ConnectionName").Delete (replace with actual name).
    • Check the Data > Queries & Connections pane: After running the macro, open this pane (Data tab) to confirm no connections remain.
    • Consider automation: Add this macro to your Personal Macro Workbook if you frequently clean connections across multiple files.

    Troubleshooting

    • Macro doesn't run or gives a compile error: Ensure you pasted the code correctly into a standard module (not a sheet or ThisWorkbook module). Check for typos, especially in QueryTable and ListObject.
    • Connections still appear after running: Some connections may be hidden or protected. Go to Data > Queries & Connections, right-click each remaining item, and check if it's enabled for deletion. If not, the workbook may have structure protection.
    • Error 1004: Application-defined or object-defined error: This often occurs when trying to delete a connection that is currently in use (e.g., a pivot table is refreshing). Close any open panes, ensure no queries are refreshing, and try again.
    • Data disappears unexpectedly: If you only wanted to remove connections but keep the loaded data, this macro is too aggressive. Instead, manually delete connections via Data > Queries & Connections and leave the tables intact.
    • Macro runs but no confirmation appears: Ensure macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings). If macro notifications are off, you may not see the MsgBox — check the VBA editor's Immediate Window for output.

    Frequently Asked Questions

    Will this delete my Power Pivot data model?

    No. This code only removes workbook connections and query tables. The Power Pivot data model is separate and unaffected unless you explicitly delete it via the Power Pivot window.

    Can I undo this action?

    No. VBA deletion actions cannot be undone with Ctrl+Z. Always backup your workbook before running the macro.

    Will this remove connections from embedded charts?

    Only if the chart is directly linked to a query table or connection that gets deleted. Charts using named ranges or static data are unaffected.

    Is it safe to run this on a shared workbook?

    Avoid running deletion macros on shared workbooks. Co-authoring may be disrupted, and changes may not sync properly. Request exclusive access first.

    What’s the difference between a connection and a query?

    In modern Excel, a "query" typically refers to a Power Query (Get & Transform) query, which creates a connection. The connection is the link to the data source; the query defines how the data is transformed. Deleting the connection removes both.

    Do I need to refresh after deleting connections?

    No refresh is needed — in fact, refreshing after deletion would cause errors if the connection is gone. The macro ensures connections are removed before any refresh attempt.