When I run defrag in command prompt is there any way to gracefully stop the process before it finishes?

Does killing the task in TM cause any problems if it is in the middle of moving a file?

Windows 7 Pro 64bit


In theory, it should be safe to just terminate the program (in your case you can use CTRL-C) because an effort is made in the design of the Microsoft Disk Defragmenter application to prevent data loss in the event of an unexpected interruption. This is documented on Microsoft's web site as follows:

The following steps are the processes by which Disk Defragmenter and
file system defragment an NTFS volume and update the MFT.

   1. Locate the fragments of each file throughout the disk.
   2. Copy the fragments to contiguous locations on the disk.
   3. Verify that the copy is an exact duplicate of the original file.
   4. Update the MFT in NTFS with the new location of the beginning of
      the file.
   5. Free up the space previously used by the file.

  Microsoft - How Disk Defragmenter Works
  (the above text can be found under "Updating the Master File Table")

Essentially, the data is copied to new sectors, the pointers to the data that was moved are updated to point to these new sectors, and then the old sectors are freed as a result. This is a very safe approach to use in case there is a crash because in a worst-case-scenario (like a crash into a blue screen) you'd have two copies of the same data, one on unallocated sectors, and the other on allocated sectors. From the perspective of reading the data, it will appear the same either way.

Additionally, there are many improvements that can be made to the Disk Defragmentation techniques that Microsoft uses (which are already more than adequate for many people), and a variety of alternative products are examined and compared in great detail here:

  Defragger (from Roedy Green's Java Glossary)

  • 1
    Excellent as usual Randolf! – Moab Aug 8 '11 at 2:39

Control C to cancel it.

I don't expect the Task manager to do any harm, since it first asks the program to close before it brute forces it.

  • Reason I asked is a few years ago I thrashed a XP installation, forcing defrag to close using the gui, it would not respond and so I killed it in TM, attempted several tries but TM finally killed it, upon reboot it was thrashed. I was impatient in those years of my life. – Moab Aug 8 '11 at 1:45
  • Yeah, killing by TM isn't always a good idea. One of the most difficult lessons I learned from C++ is that creating a process is easy. Closing a process gracefully is what separates the boys from the men. – surfasb Aug 8 '11 at 1:48
  • Its the brute force part that concerns me, its why I asked if there was a graceful method. – Moab Aug 8 '11 at 1:53
  • 1
    @Moab, just use Ctrl+C; it doesn’t kill the process since you can see it continues afterwards (it detects the keypress, aborts defragmenting, then prints out a message). I recall back in the DOS days, that there was a way to install a Ctrl+C handler (I used one in a Pascal program), so it’s no surprise that Windows-7 console apps can do it. – Synetech Aug 8 '11 at 2:01
  • @moab: don't use TM. Or get a copy of mydefrag which gives you even more granular control over your defrags. Don't worry. It uses the same APIs as defrag. Come to think of it, I can't remember the last time I had to fire up the defrag program. It had to be before Vista. . . – surfasb Aug 8 '11 at 2:10

This thread has a lot of helpful information about whether there are dangers to disks that were defragmenting during power failure (while not identical to your situation, you'd still be abruptly halting the program -- as the commenter kindly pointed out, if the system can recover from a hardware interruption, a software interruption probably won't hurt).

It's seems as though defrag is following through with the journaling that occurs during normal use.

  • 1
    I think you might want to add something like "if even a hard shut off probably won't damage it, then for a software shutdown won't" – soandos Aug 8 '11 at 1:47
  • Nice article but does not really answer my question on how to gracefully terminate. – Moab Aug 8 '11 at 1:48
  • @soandos That's a good point. Really, both ways are stopping the software before it has a chance to clean up. – jonsca Aug 8 '11 at 1:50
  • @Moab I think both of us are saying that the conventional methods of Ctrl-C or killing it from the task manager aren't going to hurt anything. Just run a chkdsk (or equivalent) to make sure. – jonsca Aug 8 '11 at 1:51
  • 1
    Yes, but this way you more directly answer the question, just something you might want to add to your answer. – soandos Aug 8 '11 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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