Virtual development environments can consume vast amounts of drive space in a short period of time for several reasons. Virtual production environments can as well — but for performance reasons dynamic virtual drives are not generally used in these scenarios — if they are, the same information can be applied, just keep in mind growth on a production server is often expected and should be planned for (there are different reasons for a different plan of attack with production machines).

The main goal of this article is in addressing drive space consumption from the use of virtual machines (Virtual Server, Virtual PC, or Hyper-V), and how to greatly reduce their sizes.

Dynamically configured VHD files grow enormously in a short period of time. This has to do with the file "breathing" as you add, remove and change files within it. In a development environment, there is a heavy amount of this type of usage which contributes to their large size.

There are several steps you can take to greatly reduce the size of your VMs.

I will walk through some of the basic steps you can take that will make a dramatic difference. For this tutorial, I will use a virtual machine running on Virtual Server 2005. The VM has SQL 2000 and IIS installed.

Remember: Only perform steps you are comfortable with — there is far more that you can do than what is listed in this tutorial (like purging sp and uninstall files and driver cache, etc.) — but these are omitted as incorrect deletions could lead to big problems later.

Let's look at what we are starting with in this example:

The current VHD file for our virtual machine is almost 78GB in size!

If we look at the volumes inside the VM, you will see they are pretty full as well.

Inside the virtual server, the two partitions equal about 68GB! Note that there is already 10GB of waste before you start anything on this particular VHD.

The first thing you need to do is to disable volume shadow copies (unless you actually are using them, which is rare).

To do this, right click on your virtual machine's c:\ drive and select "Properties" and then click "Shadow copies"

Notice how both show disabled? Well they are not — that is just the scheduling.

Click "Settings", and choose 300MB (smallest about allowed) and click OK.

When you return to the previous screen, select each volume you have (usually there is only one) and select "Disable".

This will remove your existing hidden shadow copies on the drive — potentially useful in some situations, but for the majority of users — just a big waste of space.

Notice how much space this step created on our virtual volumes!

We just went from 68GB of used space to 23.5GB — An easy first win.

Hold up? lots still to do!

Cleaning up space on the virtual volumes just leaves free space inside the VHD, once this is all this is done, the VHD will remain the same size — you still need to run a compact command on the VHD — more on that soon.

Next, remove all your install, temp, and log files (unless needed). Many of these items are useful when used initially, but result in wasted space once your work is done, so you should delete what you know you will no longer need.

Items like older log files. For example, SharePoint has them in 2 places — the "12 folder", and the "Servers" folder in "Program Files." SharePoint logs can grow enormously.

Delete IIS and other systems-type log files from "c:\windows\system32\logfiles"

Delete the download and install folders that you may have created when originally setting up your environment, if no longer needed.

Delete the "c:\windows\temp" contents (except running temp files)

Delete all the user profiles that are no longer used from "c:\documents" and "settings" (or via profiles applet)

Run the Disk Cleanup Wizard! It does not get everything that is waste, but certainly helps.

Next move on to SQL logs...

SQL log files continue to grow out of control by design, unless you specified non standard growth rates or had a maintenance plan in place that addressed your transaction logs.

SharePoint is heavy on SQL so there will be several log files that undoubtedly can be committed and cleaned up.

For this example, I will shrink the OpenWiki log file.

To start it is about 1.5GB (I have seen 30GB+ SharePoint search log files in dev!)

Open SQL Enterprise Manager and right-click the DB with the large log files (look in your data directory to decide which to shrink) and choose backup.

Note: These screen caps vary from 2000 to 2005, but they are in the same general place and look fairly similar.

Next, choose the backup transaction log files option when presented.

Backup the file anywhere — you will delete it once the shrink is successful.

Now run a database shrink.

Specify "Shrink files > log files."

Again, note that the menus look slightly different in SQL 2005.

Once you have run the database log backup and shrink — do BOTH again. SQL does not actually shrink the log file the first time, but it will the second time you backup and run shrink.

Once complete, the log file should have any actions committed to the MDF, and the vast majority of the fat purged from the LDF file.

The log file went from 1.5GB to 1MB with only a minor increase in the MDF size — the rest of the content was transitional and unnecessary.

Remember to delete the transaction log truncate backups you made — they are no longer needed.

Now empty the recycle bin if you have not done so already.

Run a disk defragment of the drives in your virtual machine.

Now you are ready to run the pre-compact tool (Virtual Server 2005 only). Hyper-V generally does not need this step, and does not provide the tool — BUT if you use checkpoints / snapshots or have in the past — you must remove the checkpoints AND download / run the pre-compact tool in your Hyper-V virtual machine prior to compacting (or it will not shrink).

From the web admin console, select "Configure Virtual Machine" option, select CD / DVD ROM drive, and "Insert an image."

Insert the "pre_compact.iso" image.

Once you select this, the wizard should autorun in your VM.

Click "OK" and wait for the utility to run (the length of time depends on drive size and fragmentation level)

Once the process is complete, shut down the VM.

Go back to the administration site page and select "Inspect disk."

For Hyper-V, shut down the VM and from Hyper-V Manager select "Edit disk."

Select the disk you want to compact, and run.

This process can take quite some time — do not interrupt it. Once it is complete, your VHD will now be reduced in file size and optimized.

In this example, my VHD went from 78GB to 11GB — an amazing space savings!!