web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Delete all worksheet e...
Power Automate
Unanswered

Delete all worksheet except one specific sheet

(0) ShareShare
ReportReport
Posted on by 3

Hi everyone and dear @abm ,

I have a SharePoint folder with lots of Excel files which anyone has one similar work sheet which name is "Major". I want to delete all other worksheets in each excel file with a flow in power automate.

thanks a lot


Categories:
I have the same question (0)
  • Mhn_Va Profile Picture
    3 on at

    What should I write in Worksheet part?
    and another problem is that the spell of major is different in different excels. (Major, major, MaJor and ....)

  • eliotcole Profile Picture
    4,390 Moderator on at

    Hi, @Mhn_Va, you can do this using the actions available from the SharePoint and Excel connectors in Power Automate.

     


     

    I should first state that it would really help us to assist your flow building for you to embed (not attach, as many cannot download files) screenshots of your flow as it is right now. This will aide anyone wishing to assist you in providing much needed context.

     

    Plus, if you really feel like adding incentive then you can include some dummy data! But that is not strictly necessary. ‌😅‌

     


    I will post a full solution soon, but you need to look at the SharePoint action named 'Send an HTTP request to SharePoint' to make custom requests. Then use that in conjunction with the V2 Sharepoint API (which is 'Graph' endpoints) to work with the Excel data in each sheet.

     

    You will be mostly concerned with the DELETE worksheet Graph API endpoint to do this, but it isn't easy to 'guess' the path needed to get there, so I will help you build it.

     

    In my solution post (to come) I will edit in, eventually, a copy / paste Scope action which you can use CTRL+V to paste into your flow by going to the 'My clipboard' tab in the old designer. I am unaware of how to paste an action in the new designer, sorry.

  • eliotcole Profile Picture
    4,390 Moderator on at

    OK, solution time, @Mhn_Va !! 🙂

     

    As I said before, once I have finished editing this I will place a block of code which you can paste into your flow. Currently I only know how to do this using the old designer, so I will leave instructions of how to paste actions in the below spoiler.

     

    Spoiler (Highlight to read)

    How To Paste Actions In To Power Automate - Designer V2

    1. Add an action then go to the 'My clipboard' tab
      My Clipboard 1.png
    2. Use the 'CTRL+V' keyboard shortcut (Windows & Linux) to paste in the copied actions
      My Clipboard 2.png
    How To Paste Actions In To Power Automate - Designer V2 Add an action then go to the 'My clipboard' tab Use the 'CTRL+V' keyboard shortcut (Windows & Linux) to paste in the copied actions

    I would also recommend that you send an email containing a report on any files that *do not* contain the "Major" worksheet by performing a separate filter on the worksheets to check FOR the "Major" sheet. If the length() of the filter body is 0 then there was no "Major" sheet.

     

     

    Solution

    I have assumed that this will be working from an automated flow running on a 'When a file is created or modified (properties only)' trigger.

     

    I have not assumed anything else, since the information is not present. There may be many things which change how this is appropriate, but being as that information is not in the original question, it is hard to work with that.

     

    General Scope

    Here is an image of the 3 actions that you need in order to remove all worksheets other than the one named "Major" which should work just fine for you:

    00 - Scope Delete Worksheets.pngI will break down each action below.

    Get worksheets

    NB: The only important thing that I would say here is to ensure that you are selecting a SharePoint library, here, not anything else. Otherwise none of this works.

    01 - Get worksheets.pngI have purposefully edited sensitive information from my examples, but you can ensure that this is all filled out correctly.

     

    If you are placing this ALL within an 'Apply to each' or 'For each' this is where anything information from that will be necessary to edit in.

    Filter array

    This filters out any sheets named "Major" by checking the 'Name' field of every worksheet listed by the previous action, and ensuring that when it does it checks it in lower case.

    02 - Filter array.pngYou can see the expression below to convert the name to lower case for the condition.

    toLower(item()?['name'])

    ForEverySheet

    This is simply an 'Apply to each' or 'For each' action using the output of the 'Filter array' action as the source to iterate over.

    HTTPS DELETE Worksheet

    This is the only action inside the loop, which will remove the sheet.

    03 - HTTPS DELETE Worksheet.png

     

    You will note a few functions / expressions in the Uri path, which I have gone into some detail in the below spoiler.

     

    Spoiler (Highlight to read)

    Site ID

    This expression pulls the Site ID from the Get worksheets action:

    split(
    	trim(
    		replace(
    			actions('Get_worksheets')?['inputs/parameters/source'], 
    			'sites/', 
    			''
    		)
    	), 
    	','
    )[1]

    From the inside out you can see this:

    1. actions() - This is a function which allows full access to an actions properties
    2. replace() - Ensuring that it has removed any existence of the sites/ relative path in there
    3. trim() - Ensuring that any erroneous leading or trailing white spaces or carriage returns are removed
    4. split()[1] - This splits the remaining text by a comma resulting in three items of which it selects the second

    Drive ID

    This expression pulls the Drive ID from the Get worksheets action:

    actions('Get_worksheets')?['inputs/parameters/drive']

    As you can see this uses the same action as the previous expression but directly to get the exact value of the the required key.

    Item ID

    This expression pulls the Item ID from the Get worksheets action:

    actions('Get_worksheets')?['inputs/parameters/file']

    Almost exactly the same expression as the Drive ID one, except here it is pointing at the 'file' key instead.

    Worksheet ID

    This expression pulls the Worksheet ID from the Get worksheets action:

    encodeUriComponent(
    	items('ForEverySheet')?['id']
    )

    Since the Worksheet ID is the one *output* of the Get worksheets action it is something that we must take from our loop. So, here, you can see:

    1. items() - The items call, here, directly takes the 'id' key value from the looped data
    2. encodeUriComponent() - This action ensures that when the 'id' value is placed into the string it is suitably formatted for an HTTP call of this nature
    Site ID This expression pulls the Site ID from the Get worksheets action: split( trim( replace( actions('Get_worksheets')?['inputs/parameters/source'], 'sites/', '' ) ), ',' )[1] From the inside out you can see this: actions() - This is a function which allows full access to an actions properties replace() - Ensuring that it has removed any existence of the sites/ relative path in there trim() - Ensuring that any erroneous leading or trailing white spaces or carriage returns are removed split()[1] - This splits the remaining text by a comma resulting in three items of which it selects the second Drive ID This expression pulls the Drive ID from the Get worksheets action: actions('Get_worksheets')?['inputs/parameters/drive'] As you can see this uses the same action as the previous expression but directly to get the exact value of the the required key. Item ID This expression pulls the Item ID from the Get worksheets action: actions('Get_worksheets')?['inputs/parameters/file'] Almost exactly the same expression as the Drive ID one, except here it is pointing at the 'file' key instead. Worksheet ID This expression pulls the Worksheet ID from the Get worksheets action: encodeUriComponent( items('ForEverySheet')?['id'] ) Since the Worksheet ID is the one *output* of the Get worksheets action it is something that we must take from our loop. So, here, you can see: items() - The items call, here, directly takes the 'id' key value from the looped data encodeUriComponent() - This action ensures that when the 'id' value is placed into the string it is suitably formatted for an HTTP call of this nature

     

     

    Full Paste Code

    Below will be code that you can paste into your Power Automate flow using the instructions which I noted at the start. You will be asked to ensure that the connections are valid.

     

    Spoiler (Highlight to read)
    {
    	"id": "f24900bb-a2ac-46a4-99e0-5d42e300b372",
    	"brandColor": "#8C3900",
    	"connectionReferences": {
    		"shared_excelonlinebusiness": {
    			"connection": {
    				"id": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness/connections/shared-excelonlinebu-PING-PONG"
    			}
    		},
    		"shared_office365groups_1": {
    			"connection": {
    				"id": "/providers/Microsoft.PowerApps/apis/shared_office365groups/connections/shared-office365grou-PING-PONG"
    			}
    		},
    		"shared_sharepointonline-1": {
    			"connection": {
    				"id": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/shared-sharepointonl-PING-PONG"
    			}
    		}
    	},
    	"connectorDisplayName": "Control",
    	"icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=",
    	"isTrigger": false,
    	"operationName": "Scope_Delete_Worksheets",
    	"operationDefinition": {
    		"type": "Scope",
    		"actions": {
    			"Filter_array": {
    				"type": "Query",
    				"inputs": {
    					"from": "@outputs('Get_worksheets')?['body/value']",
    					"where": "@not(equals(toLower(item()?['name']), 'major'))"
    				},
    				"runAfter": {
    					"Get_worksheets": [
    						"Succeeded"
    					]
    				},
    				"metadata": {
    					"operationMetadataId": "236ec8f2-a903-43a3-a099-460109d82100"
    				}
    			},
    			"Get_worksheets": {
    				"type": "OpenApiConnection",
    				"inputs": {
    					"host": {
    						"connectionName": "shared_excelonlinebusiness",
    						"operationId": "GetAllWorksheets",
    						"apiId": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness"
    					},
    					"parameters": {
    						"source": null,
    						"drive": null,
    						"file": null
    					},
    					"authentication": {
    						"type": "Raw",
    						"value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
    					}
    				},
    				"runAfter": {},
    				"metadata": {
    					"operationMetadataId": "adfefe06-03a6-4c87-b8f1-a3cbae5c954e"
    				}
    			},
    			"ForEverySheet": {
    				"type": "Foreach",
    				"foreach": "@body('Filter_array')",
    				"actions": {
    					"HTTPS_DELETE_Worksheet": {
    						"type": "OpenApiConnection",
    						"inputs": {
    							"host": {
    								"connectionName": "shared_sharepointonline-1",
    								"operationId": "HttpRequest",
    								"apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
    							},
    							"parameters": {
    								"dataset": "https://graph.microsoft.com/",
    								"parameters/method": "DELETE",
    								"parameters/uri": "v1.0/sites/%7B@{split(trim(replace(actions('Get_worksheets')?['inputs/parameters/source'], 'sites/', '')), ',')[1]}%7D/drives/@{actions('Get_worksheets')?['inputs/parameters/drive']}/items/@{actions('Get_worksheets')?['inputs/parameters/file']}/workbook/worksheets(%27@{encodeUriComponent(items('ForEverySheet')?['id'])}%27)",
    								"parameters/headers": {
    									"Content-type": "application/json",
    									"Accept": "application/json"
    								}
    							},
    							"authentication": {
    								"type": "Raw",
    								"value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
    							}
    						},
    						"runAfter": {},
    						"metadata": {
    							"operationMetadataId": "047a8141-43ea-438b-9db8-d25e29b631ee"
    						}
    					}
    				},
    				"runAfter": {
    					"Filter_array": [
    						"Succeeded"
    					]
    				},
    				"metadata": {
    					"operationMetadataId": "66d3f384-4323-4236-b40c-3d0ba618e025"
    				}
    			}
    		},
    		"runAfter": {
    			"Initialize_FileIdGraphVAR": [
    				"Succeeded"
    			],
    			"Initialize_FolderIdGraphVAR": [
    				"Succeeded"
    			],
    			"Initialize_WorksheetIdVAR": [
    				"Succeeded"
    			]
    		},
    		"metadata": {
    			"operationMetadataId": "1254234f-cd75-4ec6-bf28-8891a1f2b9bb"
    		}
    	}
    }
    { "id": "f24900bb-a2ac-46a4-99e0-5d42e300b372", "brandColor": "#8C3900", "connectionReferences": { "shared_excelonlinebusiness": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness/connections/shared-excelonlinebu-PING-PONG" } }, "shared_office365groups_1": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_office365groups/connections/shared-office365grou-PING-PONG" } }, "shared_sharepointonline-1": { "connection": { "id": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/shared-sharepointonl-PING-PONG" } } }, "connectorDisplayName": "Control", "icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=", "isTrigger": false, "operationName": "Scope_Delete_Worksheets", "operationDefinition": { "type": "Scope", "actions": { "Filter_array": { "type": "Query", "inputs": { "from": "@outputs('Get_worksheets')?['body/value']", "where": "@not(equals(toLower(item()?['name']), 'major'))" }, "runAfter": { "Get_worksheets": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "236ec8f2-a903-43a3-a099-460109d82100" } }, "Get_worksheets": { "type": "OpenApiConnection", "inputs": { "host": { "connectionName": "shared_excelonlinebusiness", "operationId": "GetAllWorksheets", "apiId": "/providers/Microsoft.PowerApps/apis/shared_excelonlinebusiness" }, "parameters": { "source": null, "drive": null, "file": null }, "authentication": { "type": "Raw", "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']" } }, "runAfter": {}, "metadata": { "operationMetadataId": "adfefe06-03a6-4c87-b8f1-a3cbae5c954e" } }, "ForEverySheet": { "type": "Foreach", "foreach": "@body('Filter_array')", "actions": { "HTTPS_DELETE_Worksheet": { "type": "OpenApiConnection", "inputs": { "host": { "connectionName": "shared_sharepointonline-1", "operationId": "HttpRequest", "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline" }, "parameters": { "dataset": "https://graph.microsoft.com/", "parameters/method": "DELETE", "parameters/uri": "v1.0/sites/%7B@{split(trim(replace(actions('Get_worksheets')?['inputs/parameters/source'], 'sites/', '')), ',')[1]}%7D/drives/@{actions('Get_worksheets')?['inputs/parameters/drive']}/items/@{actions('Get_worksheets')?['inputs/parameters/file']}/workbook/worksheets(%27@{encodeUriComponent(items('ForEverySheet')?['id'])}%27)", "parameters/headers": { "Content-type": "application/json", "Accept": "application/json" } }, "authentication": { "type": "Raw", "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']" } }, "runAfter": {}, "metadata": { "operationMetadataId": "047a8141-43ea-438b-9db8-d25e29b631ee" } } }, "runAfter": { "Filter_array": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "66d3f384-4323-4236-b40c-3d0ba618e025" } } }, "runAfter": { "Initialize_FileIdGraphVAR": [ "Succeeded" ], "Initialize_FolderIdGraphVAR": [ "Succeeded" ], "Initialize_WorksheetIdVAR": [ "Succeeded" ] }, "metadata": { "operationMetadataId": "1254234f-cd75-4ec6-bf28-8891a1f2b9bb" } } }

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 77 Super User 2026 Season 1

#2
Haque Profile Picture

Haque 68

#3
Expiscornovus Profile Picture

Expiscornovus 56 Most Valuable Professional

Last 30 days Overall leaderboard