Skip to content

Instantly share code, notes, and snippets.

@fortinmike
Last active February 11, 2024 16:56
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fortinmike/d2cf104305a6c67865caf8e6a75a6b1b to your computer and use it in GitHub Desktop.
Save fortinmike/d2cf104305a6c67865caf8e6a75a6b1b to your computer and use it in GitHub Desktop.
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
timeout /t 5
del /f /q C:\hiberfil.sys
:: 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
:: Remove some more superseded versions ("All existing update packages can't be uninstalled
:: after this command is completed, but this won't block the uninstallation of future update packages.")
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
:: 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
:: 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 the Windows Temp directory's content (fills up with evtx files with time)
del C:\Windows\Temp /f /q /a
:: 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
:: Possibly reclaim some space from the .NET Native Images
"%windir%\Microsoft.NET\Framework\v2.0.50727\ngen" update
"%windir%\Microsoft.NET\Framework\v4.0.30319\ngen" update
:: 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment