Power Query Problems

Delete Excel Power Query Queries and Connections with VBA

Muhammad TalhaMuhammad Talha Nov 18, 2026 870 views

Question details

Delete Power Query queries from an Excel workbook using VBA rather than deleting only their connections.

Delete Excel Power Query Queries and Connections with VBA
Product
Microsoft Excel Power Query
Device & OS
not provided
Scenario
A workbook contains multiple Power Query queries organized in a folder, and the user wants to remove them programmatically.
Observed behavior
Deleting items from Workbook.Connections leaves the queries in the workbook, and the macro recorder does not capture deletion of the query folder.
Before you start

In Excel, open Data > Queries & Connections and check which queries and dependent reports should remain. Save a backup copy before deleting queries, and run macros only from trusted sources.

Solution 1Recommended

Delete workbook queries through the VBA Queries collection

Use this macro to review every query in the active workbook and confirm each deletion.

Excel stores workbook queries separately from connections, so deleting a connection alone does not necessarily delete its query. This macro checks all workbook queries, not just those in one folder.

1
Open the VBA editor

Open the target workbook in Excel, then use Developer > Visual Basic to open the VBA editor. If the Developer tab is hidden, enable it in Excel's ribbon settings.

2
Insert the macro

In the VBA editor, select Insert > Module and paste: Sub DelQueries() Dim q As WorkbookQuery For Each q In ActiveWorkbook.Queries If MsgBox("Delete query '" & q.Name & "'?", vbYesNo) = vbYes Then q.Delete Next q End Sub

3
Run and review

Return to the target workbook, choose Developer > Macros, select DelQueries, and click Run. Choose Yes only for queries you want to delete; choose No to keep the others.

4
Check remaining items

Open Data > Queries & Connections to verify the remaining queries and connections. Review any remaining connections separately before deleting them.

Delete workbook queries through the VBA Queries collection
Folder and dependency limits: The macro deletes selected queries, not the folder itself. Deleting a query can affect other queries or reports that depend on it, and the operation may not be easily undone.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. It does not resolve Excel's Power Query VBA deletion issue, so use Excel for the macro above and check advanced features when moving files.

Open and edit Microsoft Excel XLSX workbooksUse a familiar spreadsheet interface for everyday workbook tasksWork with spreadsheet data in a lightweight office suiteMove compatible files between WPS Office and Microsoft Excel
QA img-9

Frequently Asked Questions

Why does deleting a connection leave the Power Query query behind?

Connections and queries are managed separately in Excel. To remove a query with VBA, delete it from the workbook's Queries collection rather than only from Workbook.Connections.

Does the macro delete only queries in a specific folder?

No. It prompts for every query in ActiveWorkbook.Queries. Choose No for queries outside the folder or for any query you want to keep.

Will deleting the queries also delete their folder?

The macro deletes the queries you confirm; it does not explicitly delete the folder. Check Data > Queries & Connections afterward for any remaining folder or connection.

What happens to reports that depend on a deleted query?

Dependent queries or reports may stop working or refreshing. Check dependencies in Data > Queries & Connections before deleting a query.