How to Completely Delete A Workflow With PowerShell

Since there doesn't seem to be a blog on this anywhere that I could find, I decided to do this on my own.  Why do I need to do this?  Because of this older blog post about a bug in SharePoint:

https://blogs.architectingconnectedsystems.com/blogs/cjg/archive/2012/06/27/Approval-Workflow-Access-Denied-_2D00_-How-to-fix-it_2100_.aspx

The migrate user command doesn't do everything that it should when moving from one domain to another.  In this case, a user had activated the workflow feature, the files are created and tagged to that user.  It is done in such a way that the migrateuser command is not able to update the user's actual acccountname (such as DOMAINAlias) in various places.  This presents a problem as that user cannot be resolved as their is not trust between the new AD and old AD.  This causes the above bug to come into play and the issue of not being able to create sites because of "user could not be found" error a reality.  So is it possible to script a fix for this? 

Yup…but it requires a bit of magic.  Since I can't seem to find any good object model method to completely delete a workflow, I went back to trusty ole sharepoint designer and "Fiddled" the traffic.  It does a super simple post to:

POST https://topleveldns/siteurl/_vti_bin/_vti_aut/author.dll 

It includes a must have header of:

X-Vermeer-Content-Type: application/x-www-form-urlencoded

 With a content body of:

method=remove+documents%3a14%2e0%2e0%2e4730
&service%5fname=%2fsiteurl
&url%5flist=%5b%5fcatalogs%2fwfpub%2fCollect+Feedback+%2d+SharePoint+2010%5de

This will fully delete a workflow from a site.  The next step is to then deactivate and reactive the workflow feature that will re-create the workflows – THIS SHOULD BE RUN AS THE SYSTEM ACCOUNT!.  The main parts of the script is this:

function DeleteWorkflow($url, $name)
{   
    $uri = new-object System.Uri("https://hubworksdev.devlab.paypalcorp.com/TeamWorks/Sites4")
    $localPath = $uri.localpath.replace("/","%2f")
    $name = $name.replace("-","%2d").replace(" ", "+")
    $postData = "method=remove+documents%3a14%2e0%2e0%2e4730&service%5fname=" + $localPath + "&url%5flist=%5b%5fcatalogs%2fwfpub%2f" + $name + "%5d"
   
    $isVermeerRequest = $true
    $res = DoPost $($url + "/_vti_bin/_vti_aut/author.dll") $postData ""   
}

function RemoveAndActivateWorkflows($url)
{
    DeleteWorkflow $url "Approval – SharePoint 2010"
    DeleteWorkflow $url "Collect Feedback – SharePoint 2010"
    DeleteWorkflow $url "Collect Signatures – SharePoint 2010"
   
    Disable-spfeature -url $url -identity "0af5989a-3aea-4519-8ab0-85d91abe39ff" -confirm:$false   
    enable-spfeature -url $url -identity "0af5989a-3aea-4519-8ab0-85d91abe39ff" -confirm:$false   
}

RemoveAndActivateWorkflows "http://topleveldns/siteurl"