Delete Excel Power Query Queries and Connections with VBA
Question details
Delete Power Query queries from an Excel workbook using VBA rather than deleting only their connections.

- 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.
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.
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.
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.
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
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.
Open Data > Queries & Connections to verify the remaining queries and connections. Review any remaining connections separately before deleting them.

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.

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.




