Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How to zero fill a virtual disk's free space on windows for better compression?

I would like a simple open source tool (or at least free) for that. It should probably write an as big as possible file full of 0and erase it afterwards. Only one pass (this is not for security reasons but for compression, we are backing up virtual machines).

Should run from inside windows and not from a disk.

On Linux I do it like this (as a user):

mkdir wipe
sudo sfill -f -l -l -z ./wipe/

Edit 1: I decided to use sdelete from the accepted answer. I had a look at the sdelete's help:

C:\WINDOWS\system32>sdelete /?

SDelete - Secure Delete v1.51
Copyright (C) 1999-2005 Mark Russinovich
Sysinternals - www.sysinternals.com

usage: sdelete [-p passes] [-s] [-q] <file or directory>
       sdelete [-p passes] [-z|-c] [drive letter]
   -c         Zero free space (good for virtual disk optimization)
   -p passes  Specifies number of overwrite passes (default is 1)
   -q         Don't print errors (Quiet)
   -s         Recurse subdirectories
   -z         Clean free space

This is an old version. I used the -c switch from the 2nd invocation and this was quite fast (syntax only valid for older versions before V1.6):

c:\>sdelete -c c: (OUTDATED!)

Edit 2: As scottbb pointed out in his answer below, there was a September 2011 change to the tool (version 1.6) The -c and -z options have changed meanings. The correct usage from 1.6 onwards is

c:\>sdelete -z c:

I have the impresion this does what I want. The sdelete tool is easy to use and easy to get.

share|improve this question
sdelete is the right tool for the job, but a September 2011 change to the tool (version 1.6) makes the above answers incorrect. The -c and -z options have changed meanings. The correct usage now is sdelete -z c:\ –  scottbb Jan 5 '12 at 3:23
Also make sure you use COW (Copy on Write) style images if you have lots of machines based off the same basic install. –  Matt Jan 5 '12 at 3:39

4 Answers 4

up vote 18 down vote accepted

On windows use the sysinternals tool sdelete to zero out all the empty space. The command you want would look like this sdelete -z c:.

Usage: sdelete [-p passes] [-s] [-q]  ...  
sdelete [-p passes] [-z|-c] [drive letter] ...  
-a         Remove Read-Only attribute.  
-c         Clean free space.  
-p passes  Specifies number of overwrite passes (default is 1).  
-q         Don't print errors (Quiet).  
-s or -r   Recurse subdirectories.  
-z         Zero free space (good for virtual disk optimization).

For Linux I suggest you use zerofree.

share|improve this answer

Windows already has a built-in command line tool that does this: CIPHER. See its /W option.

So the complete command would be: cipher /w:c: (replace c: with actual drive letter)

(it writes zeroes to the unused space, then 0xff, so you have to watch it and stop it with ctrl-c when it begins to write 0xff - not ideal, but it is free and already available in Windows since XP at least)

share|improve this answer
interesting approach. Break execution just before it starts undoing all of the zeros. +1 for a creative solution using tools that are already there! –  PsychoData Nov 12 at 16:10

SDelete will not zero out the space for compaction of a virtual disk. SDelete starts by zeroing free space, but then fills it with 0xff and then random bytes See How to prepare a Virtual Server 2005 virtual hard disk file to send to Microsoft Product Support Services For how to set up a VHD for compaction

Precompact.exe can be difficult to find for some reason. I found a seperate downloadable copy here. However if you have windows virtual PC you can also get it from %programfiles(x86)%\Windows Virtual PC\Integration Components\Precompact.iso

share|improve this answer
sdelete with the -c option is a zero fill. Perhaps you are thinking of the Windows built-in cipher utility which does not have a zero-only option? –  Jeremy M Jul 29 '10 at 5:41
"SDelete implements the Department of Defense clearing and sanitizing standard DOD 5220.22-M" (from the docs) which specifically does not simply 0 out a cluster. sdelete -c does zero out free space (after filling it), however the method that sdelete uses isn't the same as precompact and in many cases will result in a larger drive than precompact, which was designed to zero out virtual drives –  Jim B Jul 29 '10 at 12:27
This is interesting to know of another tool. Thank you –  mit Jul 29 '10 at 19:14
SDelete can leave the disk with 0s, but precompact works much faster and was meant for this sort of situation. –  Chris S Jan 5 '12 at 3:30
@romkyns It doesn't matter anymore as all modern versions of Windows and Hyper-V support trim (as of Win7 and Hyper-V 2012), there's no need to zero before compacting. The PreCompact utility only zeroed parts of the disk with deleted files, the part that hadn't ever been used was left untouched. Using something like sdelete would fill the dynamic disk to static size, which could then be compacted back down to only the size actually used. –  Chris S Sep 15 at 13:45

Why third party tools, when Windows already includes all you'll ever need, for "free" and as fast a it can ever be:

1) format X: /fs:XXXX /p:#, where X is the drive letter; XXXX is the file system,Yeither and # is the number of passes;

2) diskpart clean command: http://technet.microsoft.com/library/cc766465.aspx, http://support.microsoft.com/kb/300415;

Btw, you don't need more than a single pass, either - a claim otherwise is either outdated info for obsolete disk red/write strategies, people pushing products, paranoia driven, or bs passed on as "expert advice".

Edit: nvm, missed the free space part...

share|improve this answer
In many cases there are third party tools available that do good or better jobs, are easier to install or more reliable. But this is totally OT, the question was not about who writes it. Apart from this Microsoft aquired Sysinternals (who wrote SSDelete) because they were lightyears better than the builtin stuff. Just compare ProcessExplorer to the MS product "TaskManager" -1 for starting a flamewar –  Alojz Janez Jul 11 '13 at 10:49
You missed the "open source" requirement in the question, did you? –  Alojz Janez Jul 11 '13 at 10:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.