Cleanup target directory before deployment

Is there a way to for the target directory to be cleaned up before package extraction occurs? I’m having some problems where artifacts from previous deployments are being left behind in certain environments after they’ve been removed from a package. Is there a setting somewhere for this, or do I have to create a pre-deploy powershell script for that?

Hi Bob,

If you are deploying the same version of a package (for example, Mypackage.1.0.3 was installed, and you are re-deploying Mypackage.1.0.3) Octopus should already clean the target directory before installing - please let me know if it isn’t, and post a copy of the build output.

If you are deploying Mypackage.1.0.5, and previously Mypackage.1.0.3 was installed, Octopus will use a different folder - so there shouldn’t be any items left over again. However, if you have items in a shared location that you want to uninstall, you can use a PreDeploy.ps1 script

Hope that helps,



I am/was deploying different package versions, but it was for projects for which I’m using the “OctopusPackageDirectoryPath” variable, so it always deploys to the same directory. I had packages that had some artifacts in them that were removed from subsequent packages, and they were getting left behind, when I didn’t want them to be. It was a relatively simple workaround to manually remove the unwanted artifacts, but seemed like something that should be automated. I definitely understand that this could be done with a PreDeploy.ps1, but I was mostly wondering if there were some setting that I wasn’t aware of that would force the directory to be cleaned out automatically prior to package extraction.

Thanks for the reply.


Aha. A setting to clear the path in OctopusPackageDirectoryPath before installation is a good idea. I’ll add it to the backlog.


I agree a setting would be appropriate here for our shop. Unfortunately we have some old websites where not all files in the document root are under version control, so in some cases we would want Octopus to continue its current behavior of expanding a package over an existing site instead of cleaning it first.

Did this ever get done and released?

I use a predeploy script to do this:

if ($PurgePackageDirectoryBeforeCopy -and (Test-Path $OctopusPackageDirectoryPath))
Write-Host "Flag is set to purge directory - deleting files."
Get-ChildItem $OctopusPackageDirectoryPath | foreach ($) { if ($.name -ne “SearchIndex” -and $.name -ne “Logs”) {remove-item $.PSPath -force -recurse} }

This deletes all the items and folders in the deploy directory except for my “SearchIndex” and “Logs” folders and their contents.

Hi dmiser,

Yes, it did, the special variable is called “OctopusPurgePackageDirectoryBeforeCopy”. The default is false, but if set to true the directory will be purged. It is documented on the variables page.


Is this recursive as it still doesn’t work for me?

After renaming/deleting script files around in \Scripts I see the step ‘Purging the directory’ but the old versions are still there after the deployment.

I have checked and they are no longer in the deployment package.


Ignore my previous message. Someone had been fiddling with other settings. It is working now.

The flag works fine to get rid of files, but it appears not to be able to remove old directories. I can see the Purge step inside the log, and files do get cleaned up.

Not a huge deal for me now, since I can handle that on the PreDeploy step, but I would expect the purge to do away with everything prior to the deployment.

Suggestion: be able to define some files or folders that must be left untouched :slight_smile:


+1 for the suggestion to allow for some files/folders to be left untouched.