Explained: How to Defrag a Network Drive (NAS or Remote PC)

Dennis Faas's picture

Infopackets Reader Ron C. writes:

" Dear Dennis,

I have several drives on my network that I'd like to defrag, but none of the defrag software programs I've tried allow me to defrag a network drive. Any suggestions on how to defrag a network drive, or a program that can provide this functionality? "

My response:

The reason you can't find a program to defrag a network drive is because this is not possible - the easiest way to explain "why" is that the remote drives are owned by another operating system. Simply put: your system does not have permission to move these files around. With that said, there is a workaround to defragging a network drive, and I'll explain in detail below.

Why Defragging a Network Drive isn't Possible

First, I'll explain why defragging over a network won't work.

Let's assume that your wife has a PC on the network and she's given everyone on the network access to one of her folders. Normally if you go to "My Computer" or "This PC", then click on the "Network" icon, then click on a specific network computer, Windows will display all the available shared folders pertaining to a particular PC. If you then right click an available shared drive (or folder), you can map the network drive to your PC - let's call it drive "Z". Now, when you click on "My Computer" or "This PC" the mapped network drive or folder is displayed as drive "Z", alongside your "C" and "D" drives (for example).

Even though the network "Z" drive appears as if it were part of your system, the truth is that your wife's PC still "owns" this drive as far as file permissions are concerned. When it comes to file permissions, the most important thing to realize is that certain files on your wife's PC will be constantly in use (loaded into memory) by the operating system, and this list changes as each second passes.

With that said, there is no way for a defrag program running on your local PC to know which files are in use on the remote PC. As such, these operating system files on the remote PC would be considered "locked" because they are resident in memory, and therefore unmovable. The way defrag works is to move files from one location on the drive to another so that the files are placed in a contiguous (side by side) manner - whether you're defragging according to most accessed files, or to optimize free space. Since the defrag program running on your local PC can't move certain files on the remote PC (because they are locked), the defrag would not be able to complete.

With that in mind: even if you could defrag a network drive, the data would have to be read from the remote drive, then sent to your local PC over the network for temporary holding until the defrag program figures out where to put it back (which takes time to transfer!); once that is figured out, the data would have to be read from your local PC, then transferred back over the network (which takes more time!), then stored on her drive. Since most computers these days have anywhere from twenty thousand to a few hundred thousand files, defragging over the network would likely take days to weeks to complete. In other words, it's not a good idea!

So, what is the proper way to defrag a network drive?

The simple answer is to have the remote drive defragged by the owner of that drive.

In other words, if you are sharing a folder on your wife's PC over the network and you want to defrag this folder (or drive), then have the wife run a defrag on her own system. That way, the defrag program and the operating system can communicate with one another to decide which files are OK to move and which ones are not. If you need the files defragged constantly, you can likely find a good freeware defrag program that is capable of running on schedule, then have your wife (or yourself) implement a schedule.

Defrag using NAS (Network Attached Storage)

If the drive you're sharing on the network is attached using NAS (network attached storage) - for example, a hard drive directly connected to a router - and you need that drive defragged, then you would have to safely disconnect the drive from the NAS and reconnect it to a PC, then run the defrag that way, then re-attach it to the NAS.

A word of warning, however: some NAS systems use a proprietary operating system (usually Linux), and defragging the drive in the manner I just described won't work. In that case you would have to get the technical specifications of the NAS and defrag using a similar operating system. Optionally, the NAS may be able to defrag the drive(s) natively within its own system - you would need to connect to the web administration control panel to see if that option is available.

I hope that helps.

Got a Computer Question or Problem? Ask Dennis!

I need more computer questions. If you have a computer question - or even a computer problem that needs fixing - please email me with your question so that I can write more articles like this one. I can't promise I'll respond to all the messages I receive (depending on the volume), but I'll do my best.

About the author: Dennis Faas is the owner and operator of Infopackets.com. With over 30 years of computing experience, Dennis' areas of expertise are a broad range and include PC hardware, Microsoft Windows, Linux, network administration, and virtualization. Dennis holds a Bachelors degree in Computer Science (1999) and has authored 6 books on the topics of MS Windows and PC Security. If you like the advice you received on this page, please up-vote / Like this page and share it with friends. For technical support inquiries, Dennis can be reached via Live chat online this site using the Zopim Chat service (currently located at the bottom left of the screen); optionally, you can contact Dennis through the website contact form.

Rate this article: 
Average: 5 (1 vote)


jamies's picture

Considering the 'want to' defrag a NAS data store.
There are several practicalities to consider before embarking on such a project:
Firstly, is the NAS using 1 drive, or RAID with data mirrored, or spread over several drives (Striped).
If the drives are in a 'RAID' setup, then they will be optimised to speed reads, but writing data will take longer than a normal drive.
Also -
Modern data stores my well be SSD, where defrag is not likely to make data reading any faster, and - again writing is much slower

Even if the drives are spinning platter ones, modern ones may well be able to move the heads to new locations much faster than the disk can spin, and may be optimised for use in that way to service a number of users wanting access to their 'stuff' fast.

Then there is the 'OS' and partition storage/file management format
and by OS I mean anything from DOS through the various versions of Windows, Mac OS's Linux, or - even, maybe a mainframe - or server hive with hundreds of drives.

OK - assuming it is a locally attached drive (plugged into the local hub, directly or via a PC and 'WORKGROUP'?), and only used by you and others in that building

So - if you still want to defrag -
Next consider the type of files on the drive - some OS's will assemble groups of small files and put the group into a single space-allocation, or for NTFS the small files may be held within the MFT - on the basis that the file is smaller than the control data needed to allocate a storage area for it within the file-allocation-space.

Large files - may well be on the drive in contiguous blocks - some OS's file management facilities are relatively intelligent - and will, providing they are not having to record several large files at the same time, manage to put large files into a single contiguous allocation, or maybe a few large ones.

Now - back to considering the small files ( such as those associated with web browsing) - many are small, or even just filename entries with no data -

If the file management facility ( partition format) is FAT - then there is a space allocation block - 1 entry for each block that can be allocated for a file's data
and a series of files (marked as 'Directories') that have a fixed format for the entries in the file. Those entries can be sorted , and the directories may be spread over several blocks of allocated space - that space being made up of the first available block found in the space allocation table when there was more space needed to add another entry into the directory (note the entries in that directory will be for the old 8.3 type names with several entry spaces being used for the long names almost everyone (except for the windows OS) prefer to use now.

So - accessing files under FAT - find the file containing the first level directory,
read through it until you find the required subdirectory - or filename
remembering that the directory 'files' will probably be spread throughout the space of the drive - Norton utilities used to have an option to group those entries into a contiguous block - saving loads of head movements and waits for the bit of the directory file to be read. Norton utilities also had an option to move set filetypes to the middle, or the end of the drive space -grouping them together and leaving space for new files to be grouped into the larger areas of space moving those files had created.

Now - AFAIK there is no similar facility for NTFS.
I believe windows will do tidying-up of the MFT when the system is not busy (whenever that may be) - but do remember that as you add lots of small files, that the MFT will become larger. and - for many people finding file entries in the MFT takes much more time than actually reading the file.
So - a simple process of clearing the browser files may speed up your system enormously -
remember browser storing of files was on the basis that download speeds meant it was, maybe a second to get each of the many small files that may make-up a web page.
With your web speed - do you need to have a large browser storage area 9 well - lots of entries clagging up searches within a large MFT that will no longer fit into the available memory.

OK -

Back to DEFRAG -
You still want to do it -
Yes - but do you NEED to do it

OK - You still want to do it -

A possibly useful technique - if there is a reasonable amount of space on the storage - manually do what a defrag would do -
copy files to a new location on the store - and carry on doing that - folderset by folderset until you have moved all you believe is worth trying to deal with - larger files, or groups of files used at the same time by a process.
And - in order to do that reasonably safely:
1) Ensure you are the sole user of those files on the NAS
2) Backup those files to an additional location NOT on the NAS
3) Rename the folders or individual files to be moved - the higher level folder you can rename the easier the task will be
4) Recreate the original filenames by copying the renamed versions to newly created versions of the original folders.
5) delete the renames sets.

That will force the NAS file/storage management facility to create new versions of the files - using the freespace - which will (hopefully) be in larger 'lumps' towards the end of the drive's space. (Remember Norton could do the moves)
Then deleting the originals will free the space those files would have been using.

Sounds reasonably simple

BUT - remember the bit above about the OS using the short 8.3 names -
And - the killer - if you create files using the longnames - Windows allocates those files shortnames it generates (4 U)
Now - the modules of the OS - and some applications software will have noted the shortname allocated to a file when the facility was installed - and despite the file being given a new short name as it was re-created, or copied, or even just moved - the original shortname will still be used - and that will, almost certainly either not exist, or be associated (pointing to) a different file

So - doing the above 'manual' process on anything other than data files will almost certainly lead to the OS or applications no longer working.

Now - there is a way to (try to) avoid that -
DOS Copy, XCopy and windows - copy/cut and paste or drag'n'drop all ignore the shortname, and get the OS to create new ones - and they will be new ones as the OS does not allow there to be duplicate 'full-names'
BUT - Robocopy, XXcopy and almost all 'partition type backup facilities can retain the shortname, and - providing the file is 'restored' in the location that the original file was in, the original shortname will be reinstated ... providing, that is, no part of the path or actual filename has already been allocated a different name - Ha! yes - another set of Microsoft Features' to make life so much fun for all

Then deleting the originals will free the space those files would have been using - allowing the newly created files to be fragmented into the space now freed.
- Back to needing to defrag!!

If the format is NTFS - then all the file entries are maintained in 1 space allocation set - called the MFT - and ther will be lots of the