Faced with an issue of rapidly running out of free disk space on our IIS web servers, one evening I set about creating a Powershell script to automate the process of removing unused website deployments from our web server farm. Previously it had been a manual process to clean-up these now unused deployments which were left behind by our versioned deployment process. As you can imagine this task was a time consuming process that previously had no automated solution that was smart enough to detect which builds were no longer needed.
The outcome to resolving this issue was the below script. If executing the script without the ‘-delete True’ parameter the script will run in a preview style mode and will list builds it would have removed. This is great as you can do a manual check before allowing the script to delete actual files.
Note - This script assumes you websites folder structure will look like this [WebsiteName][WebsiteBuild]Content and that the physical path in IIS for the website is set to the ‘content’ folder.