I am trying to delete empty folders after the documents have been moved/deleted. I have the current workflow set up to search repository because it needs to look through sub folders also. Based on a metadata field, it will either move the entry or delete it. The folders will usually contain two documents, where one will be moved and one will be deleted. My current workflow will do exactly that except delete the folders after they become empty. Attached is my current workflow. I have the delete entry set to delete empty folders only too. The problem is that it needs to run through both branches before the folder becomes empty.
Question
Question
delete empty folders after docs have been moved
Answer
Okay that makes sense, but it should be a Conditional Decision, not a Conditional Parallel because you want OR, not AND/OR conditions.
Based on that information, you have nothing in the process that would delete the folder, just the document.
You still need a delete entry in the place I described to target the folder.
Replies
Usually I handle this type of thing with a recursive workflow using the following process
- Find all entries in the parent folder
- Conditional Decision
- If the entry is a document - perform the document action (move, etc.)
- If the entry is a folder - invoke the same workflow on the child folder (set it to wait for the workflow to complete before proceeding)
- At the end of the workflow, add the Delete Entry activity targeting the starting entry (folder) and set to only delete empty folders.
- Conditional Decision
What this does is loop through every single entry in the chain. If the entry is a folder, it invokes the same process again to drill down into each subfolder. At the end of each loop/workflow instance, it will attempt to delete the folder, provided it is empty.
Because the process recursively loops through nested folders, you would be deleting the lowest level folders first so folders wouldn't fail to delete because they contain empty child folders.
Hi Jason,
The parent folder won't contain any documents. It just contains subfolders that contain the documents. Below is my current folder structure. The documents will be either moved from the Returned Mail subfolders or deleted.
If you don't need to worry about nested folders, then you could do it all in one workflow.
Just do the following
- Find Entries to grab all the folders in the starting entry
- For Each Entry to loop through each one
- Find Entries to get the documents in the Current Folder/Entry
- For Each Entry on each document
- Move your document
- Delete Entry (Current Folder/Entry set to only delete empty folders)
- Find Entries to get the documents in the Current Folder/Entry
Basically, you look through each folder, have a nested loop for each document, at the end of the first loop (after the second loop for the documents is complete), you attempt to delete the current folder.
Sorry, I must be missing something. Find entries doesn't look through subfolders? That's why I had to go with search repository. I need it to look through the returned mail folder and subfolders only.
That's the point of the nested For Each Entry loop. The Find Entries gets the folders in the starting folder.
Then the second loop is within the first loop and uses the "ForEachEntry_CurrentEntry" to search the contents of each folder you found with the first Find Entries.
Like so,
Find Entries on its own will not search subfolders, but when you nest them in a loop you can create a recursive process that will.
The problem with using a Search activity is that it returns the results kind of like a "flat" collection so you have to do a lot more work to identify the empty parent folders.
The workflow is getting stuck at the conditional routing. It's not picking up the documents in the 2nd find entries even tho i selected documents. I'm sorry, I'm still new to workflow.
The configuration is wrong on the second Find Entries. You have it set to Find Entries "In the same folder as the entry"
But it should be set to "In the folder" as seen in my screenshot.
It was originally set to "in the folder", but when that didn't work i changed it to "In the same folder as the entry" I switched it back and its still stuck on the conditional routing part.
What are the settings for your For Each Entry and For Each Entry 2 activities? I know this configuration works because I've used it in several workflows.
Thanks!
Mui,
The Delete Entry activity should not be inside of the second loop; I'm not sure I understand the point of the conditional branch because it would be acting on the folder repeatedly for every document, and it would still fail because the last document to be moved would occur in parallel.
Instead of the conditional branch, set it up like my example where the delete occurs after For Each Entry 2, but inside of For Each Entry, and acts on the Current Entry for the first loop.
Okay that makes sense, but it should be a Conditional Decision, not a Conditional Parallel because you want OR, not AND/OR conditions.
Based on that information, you have nothing in the process that would delete the folder, just the document.
You still need a delete entry in the place I described to target the folder.
GOT IT!! Thank you so much for all your help Jason. That totally fixed the issue.
No problem, glad you got it working!
In your original workflow, where are you deleting the document in that folder?
Hi, I found the mistake. It was in the branch conditions. It was set to "current entries" and not "current entries 2." It is now doing the conditional routing, however it is still not deleting the empty folders after the documents are gone.
Right, it looks like you're deleting the document, but you don't have an activity to delete the folder.
How do you know when these documents should be deleted? Is this just a periodic purge of documents or can these folder enter the "final" stage at different times from each other?
It's a periodic purge. They usually will enter the final stage at the same time. The workflow is now working correctly. This is the final workflow.