Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aggressively reclaim disk space in a Windows partition
::
:: Reclaims Windows disk space in a "destructive" manner (can't uninstall service packs and updates afterwards, etc.).
:: Use at your own risk. Useful for Windows installations in space-constrained environments, such as a small Boot Camp
:: partition on a Mac.
::
:: [IMPORTANT] It is strongly suggested to make a full-disk backup of your Windows partition before running this script,
:: as you can't roll back service packs and updates afterwards.
::
:: [IMPORTANT] Run this script as admin (required to run `Dism.exe` among other things).
::
:: [OPTIONAL BUT RECOMMENDED]
:: Run `cleanmgr /SAGESET:123` from cmd.exe and choose everything you want to clean before running this script.
:: This allows cleaning things such as `Windows.old` and other large, unnecessary directories.
::
:: Disable Hibernation (removes hiberfil.sys)
powercfg -h off
:: Remove temp files (will probably log warnings for files that are in use,
:: but will remove lots of things nonetheless)
rd %temp% /s /q
:: The $GetCurrent directory is created during the upgrade process. It contains log files
:: about that last Windows upgrade process and may also contain the installation files for that update.
rd C:\$GetCurrent /s /q
:: If you choose to delete the $WINDOWS.~BT folder on Windows 10, you won’t be able to downgrade
:: to the previous build of Windows 10 or previous version of Windows your PC had installed.
rd C:\$WINDOWS.~BT /s /q
rd C:\$WINDOWS.~WS /s /q
:: The /Cleanup-Image parameter of Dism.exe provides advanced users more options to
:: further reduce the size of the WinSxS folder. All existing service packs and updates
:: cannot be uninstalled after this command is completed. This will not block the
:: uninstallation of future service packs or updates.
Dism.exe /online /Cleanup-Image /StartComponentCleanup
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBasecmd
:: To reduce the amount of space used by a Service Pack:
:: remove any backup components needed for uninstallation of the service pack.
Dism.exe /online /Cleanup-Image /SPSuperseded
:: Run the StartComponentCleanup task in Task Scheduler to clean up and compress components
schtasks.exe /Run /TN "\Microsoft\Windows\Servicing\StartComponentCleanup"
:: After you have installed all the pending updates, it is safe to delete all the
:: files and folder under C:\WINDOWS\SoftwareDistribution\Download\
:: https://superuser.com/a/53274
net stop wuauserv
del /q C:\WINDOWS\SoftwareDistribution\Download\*
for /d %%x in (C:\WINDOWS\SoftwareDistribution\Download\*) do @rd /s /q "%%x"
net start wuauserv
:: Delete our own Temp directory
rd C:\Temp /s /q
:: Delete Logitech G Hub caches
rd C:\ProgramData\LGHUB\cache /s /q
:: Delete Windows memory dumps (not useful unless you're having specific problems to diagnose)
del /q C:\Windows\MEMORY.dmp
:: Run Disk Cleanup in "System" mode, without displaying the window first
:: (a final success message is still displayed)
cleanmgr.exe /verylowdisk /d c
:: Requires preparing preset "123" by running `cleanmgr /SAGESET:123` beforehand
:: and selecting which options you want.
cleanmgr.exe /SAGERUN:123
:: Reclaim reserved storage
:: https://www.windowslatest.com/2020/03/15/windows-10-will-finally-allow-you-to-reclaim-reserved-storage/
:: Windows 10 2004 only
DISM.exe /Online /Set-ReservedStorageState /State:Disabled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment