
In my solution to populate a dynamic Word template I save and extract the Word-document as zip file in a temporary folder in OneDrive (for Business), and at the end of the flow I delete the folder with its content. But there has been a change to the system that will prevent the Delete folder step due to an error (see message below), so I had to come up with a solution for that.
"You have to delete all the items in this folder before you can delete the folder."
These are the extra steps that should be added to the flow prior to the final “Send an HTTP request to SharePoint | Delete folder” action.

Before the flow can delete the temporary folder itself, we need to delete its content (every single file and sub folder) first. We can use the output of the “Create file” and “Extract archive to folder” actions to get the ids and paths to be able to do this.

First I add a Levels property to the outputs body of the “Extract archive to folder” action. The Levels number is used to sort the paths (of the sub folders) from the lowest to the highest level within the temporary folder.
addProperty(item(),'Levels',length(split(item()?['Path'],'/')))

The input for the “Apply to each | File” control is the array with the properties of the extracted files, sorted based on the Levels property. Note: the sort order of the files doesn’t really matter in case of deleting 
reverse(sort(body('Select_|_Add_Levels'),'Levels'))
Every file from the array is then deleted, based on its “Id”.

Then we delete the Zip file itself. Now all files are deleted and only the temporary folder and its sub folders are left.

We use the paths of the files to subtract the path of their folder. The sort order is essential to be sure the lowest level sub folders will be deleted first.

substring(item()?['Path'],0,lastIndexOf(item()?['Path'],'/'))
To get an array of unique paths we use the “union” function; this is the input for the “Apply to each | Folder” control.

union(body('Select_|_Paths'),body('Select_|_Paths'))
Then every sub folder is deleted using its path that is a concatenation of the base URL of the OneDrive (for Business) and the path from the input of the “Apply to each” control.

concat('/personal/st***************_com/Documents',item())
Last but not least the existing “Send an HTTP request to SharePoint | Delete folder” action will delete the temporary folder itself, now it’s empty the action will be succesful.
The post Delete all the items in a OneDrive folder before you can delete the folder itself with Power Automate flow appeared first on There's Something About Dynamics 365.






