Use Excel VBA to delete all external data connections and Power Query queries by iterating through the Workbook.Connections and ThisWorkbook.Queries collections and calling Delete on each. This clears queries and connections without closing or recreating the workbook, helping clean up files before sharing or archiving.

What You'll Need

  • Excel installed (Microsoft 365, Excel 2016 or later)
  • A workbook containing at least one query or connection
  • Developer tab enabled in Excel
  • Basic familiarity with the VBA editor

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 Visual Basic for Applications (VBA) editor.
  3. In the VBA editor, click Insert > Module to create a new standard module.
  4. Copy and paste the following VBA code into the module window:
    Sub RemoveAllQueriesAndConnections()
        Dim conn As WorkbookConnection
        Dim q As Object
        
        ' Disable screen updating for speed
        Application.ScreenUpdating = False
        
        ' Remove all Power Query queries
        On Error Resume Next
        For Each q In ThisWorkbook.Queries
            ThisWorkbook.Queries(q.Name).Delete
        Next q
        On Error GoTo 0
        
        ' Remove all workbook connections
        On Error Resume Next
        For Each conn In ThisWorkbook.Connections
            conn.Delete
        Next conn
        On Error GoTo 0
        
        ' Re-enable screen updating
        Application.ScreenUpdating = True
        
        MsgBox "All queries and connections have been removed.", vbInformation
    End Sub
  5. Close the VBA editor and return to Excel.
  6. Press Alt + F8 to open the Macro dialog box.
  7. Select RemoveAllQueriesAndConnections from the list and click Run.
  8. Wait for the message box confirming completion. Refresh the workbook if needed (press F9 or use Data > Refresh All to verify no connections remain).

Common Mistakes

  • Running the macro on a shared workbook—connections may fail to delete due to lock conflicts. Save and close any other users’ instances first.
  • Using ThisWorkbook.Queries without checking if Power Query is loaded—older Excel versions (pre-2016) may not support it. Use Application.Version checks if targeting legacy Excel.
  • Assuming all queries appear in the Queries collection—some queries embedded in worksheets (e.g., via Get & Transform Data with From Table/Range) may not show here. Those are part of ListObjects and require separate handling.
  • Not disabling screen updating—while minor, this can slow down deletion in large workbooks with many connections.
  • Running the macro without saving first—if the workbook crashes mid-execution, unsaved changes (including connection data) could be lost.

Tips

  • Before running the macro, export your queries by right-clicking each in the Queries & Connections pane and choosing Export if you need to re-import them later.
  • To delete only specific connections, modify the loop to test conn.Name or conn.Type before deleting.
  • For large workbooks, add a progress indicator using Application.StatusBar inside the loop for better feedback.
  • After deletion, check the Data > Queries & Connections pane to confirm nothing remains—no queries or connections should appear.
  • Use On Error Resume Next cautiously—only around the deletion loops, and always follow with On Error GoTo 0 to restore normal error handling.

Troubleshooting

If the macro fails or does not remove all items, follow these steps:

  1. Check for protected sheets or structure: If the workbook is protected, connections cannot be deleted. Go to Review > Unprotect Sheet or Unprotect Workbook, then re-run the macro.
  2. Verify Power Query is enabled: In Excel, go to File > Options > Add-ins, ensure Power Query is listed and active. If not, enable it and restart Excel.
  3. Look for hidden queries: Some queries may be tied to PivotTables or charts. Delete associated PivotTables or charts first, then re-run the macro.
  4. Check for external links: Press Ctrl + Alt + F9 to force a full recalculation, then check Data > Edit Links. Remove any remaining external links manually before re-running the macro.
  5. Use the Immediate Window for diagnostics: In the VBA editor, press Ctrl + G to open the Immediate Window. Add Debug.Print ThisWorkbook.Connections.Count before and after the deletion loop to confirm how many connections exist.
  6. Reset the VBA project: If the macro throws compile errors, go to Debug > Compile VBA Project. If it still fails, copy the code into a new module in a blank workbook to rule out corruption.

Frequently Asked Questions

Will removing queries delete my data?

No—removing queries only deletes the query definitions and connections. Any data already loaded into worksheets (e.g., PivotTables, tables, or ranges) will remain, but it will no longer refresh automatically.

Can I undo this action?

Not directly in Excel. Once deleted, you must re-import queries using Data > Get Data or recreate them. If you exported queries beforehand, you can re-import them from those files.

Does this work on shared workbooks?

No. Shared workbooks do not support VBA modifications to connections or queries. Unshare the workbook first via Review > Share Workbook (uncheck Allow changes by more than one user), save, and then run the macro.

Why does Excel ask to update links after running the macro?

Some formulas or PivotTables may still reference deleted connections. The prompt means Excel is trying to resolve broken references. Delete or update those dependent elements manually.

Do I need admin rights to run this?

No—only standard user permissions are required. However, if the workbook is stored on a network drive with restricted write permissions, saving changes after deletion may fail. Ensure you have write access to the file location.

Will this work in Excel Online?

No—VBA macros are not supported in Excel Online. Use Excel Desktop (Windows or Mac) to run this macro. You can open the file in Desktop, run the macro, and save back to OneDrive or SharePoint.