How to delete old pagefile.sys and hiberfile.sys on secondary disk (old windows install)

A while ago I swapped my main hard disk for a SSD. Now the old one is used as a secondary hard disk, and my OS is a fresh windows install on the main SSD disk.

Nevertheless, there are still huge pagefile.sys and hiberfile.sys on that secondary hard drive. Those are not the ones used by the current windows, as those do exist on C:\.

I tried to attrib -s -h them, but it refused with "Access denied".

Any idea how to delete those old unused system files and reclaim the space?

6

7 Answers

The files are protected using NTFS ACLs. Use takeown to take ownership of them.

4

Combining the answers it's quite simple, no external programs needed.

From a dos prompt:icacls hiberfil.sys /grant everyone:f to get access to the fileattrib -h -s hiberfil.sys to make it visible for deletiondel hiberfil.sys to delete the file

For pagefile.sys it works the same way.

2

Use unlocker to do this. It can not only remove the locking handles that are giving you an "access is denied" message, but can also delete the files at the same time (in case the program tries to reacquire the handle).

1

I had tried all these methods since migrating my OS to an SSD a year ago. Takeown seems to work, but the file cannot be found to delete, it does not show up doing a DIR from the command prompt, in file browsers, ect... Hiberfil is already disabled via command prompt, but I tried to re-enable it and disable it, still no go.

It was like the file was there, but not there.

What worked for me was creating a 'New > Text Document', Renaming that to Hiberfile.sys. I had to do this in a folder because it would not let me create anything except for a folder in my main F: drive. I then Paste it to F:\, then deleting that file. Freed up the 32GB I had tried to get back a few times over the past year.

1

If you have only one drive, or you want to maintain the integrity of the files on your secondary drive (i.e. so you could later boot into it if needed), you'll want to disable the pagefile first (steps to disable pagefile from link).

If you don't care about preserving the drive, skip to step 5.

  1. Press Win+R (or Win+S) → enterSystemPropertiesPerformance.exe → press Enter → Click Advanced tab.
  2. Click Change under Virtual Memory and uncheck Automatically manage
    paging file size for all drives.
  3. Click on the partition you are trying to resize and set paging file size to No paging file. Click Set, then OK.
  4. Restart. You'll notice that both pagefile.sys and swapfile.sys are now gone from your disk.
  5. If the page file is still there, use Unlocker to delete the page file on restart.

I'm adding step 5 because the article seems to imply that the page file will disappear as soon as you restart.

This might not always be the case. When I restarted Windows 7 the file was still there. I couldn't delete the file because Windows 7 claimed the file was in use.

TAKEOWN didn't solve the problem either. Unlocker initially said that no locking handle was found.

However, Unlocker did give me an option to delete the file on restart, which worked great. @Kevin, Thanks for suggesting Unlocker.

2

You probably need to take ownership. And maybe remove the read-only setting? Re taking ownership: this can be achieved manually, but I prefer to add an (extended, hold shift) context menu.

I used the super useful SysInternals tool to locate my entries, F6 takes me to the registry where I can export the entries. You can copy and paste these into .reg files, save then open them to quickly add :)

Take&ownership-all.REG

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\runas]
@="Take &ownership"
"HasLUAShield"=""
"NoWorkingDirectory"=""
"Extended"=""
[HKEY_CLASSES_ROOT\*\shell\runas\command]
@="cmd.exe /c takeown /f \"%1\" && icacls \"%1\" /grant administrators:F"
"IsolatedCommand"="cmd.exe /c takeown /f \"%1\" && icacls \"%1\" /grant administrators:F"

Take&ownership-Directory.REG

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Take &ownership"
"HasLUAShield"=""
"NoWorkingDirectory"=""
"Extended"=""
[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
"IsolatedCommand"="cmd.exe /c takeown /f \"%1\" /r /d y && icacls \"%1\" /grant administrators:F /t"
1

I used the commands I had already, mixed with some from this thread, specifically
user1686 had the missing command I needed. Here are the commands I used to make this work from a command prompt. I did this from WinPE, so I am not sure if it will work the same in Windows.

pagefile:

attrib -s -h drive:\pagefile.sys
del drive:\pagefile.sys

Hibernation file:

takeown /f drive:\hiberfil.sys
icacls drive:\hiberfil.sys /grant everyone:f
attrib -s -h drive:\hiberfil.sys
del drive:\hiberfil.sys

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like