How to Run Disk Cleanup Command Line for Silent Execution

You may use CMD or Powershell easily to automate the Disk Cleanup process

Reading time icon 4 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

Run Disk Cleanup with command line for silent execution

Keeping your Windows system running smoothly often involves regular disk cleanup. Automating this process can save you time and ensure your system remains clutter-free without manual intervention. Here’s how you can achieve silent disk cleanup using command line methods.

How do I run Disk Cleanup command line for silent execution?

1. Using the /VERYLOWDISK Switch

  1. Press the Windows key, type cmd, then click on Run as administrator from the search results.
  2. Enter the command: cleanmgr.exe /VERYLOWDISK
  3. This command will clean up all files automatically and show a dialog with the amount of freed-up space, but does not require user interaction during the cleaning process.

This method helps clean up unnecessary files effectively without needing manual confirmation, making it ideal for automated scripts.

2. Using /SAGESET and /SAGERUN for Presets

  1. Create a Preset for Disk Cleanup:
    • Open Command Prompt as an administrator.
    • Execute cleanmgr.exe /SAGESET:1
    • Select all desired cleanup options in the dialog box.
    • Click OK to save the preset settings.
  2. Run the Saved Preset:
    • Use the following command to run the previously saved settings automatically: cleanmgr.exe /SAGERUN:1

This technique allows you to configure and reuse cleanup settings without any manual intervention on future runs.

3. Combining /VERYLOWDISK in a Script

  1. Press Windows key, type Notepad, and open it.
  2. Type or paste the following script: @echo off set rootkey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches for /f "tokens=*" %%K in ('reg query %rootkey%') do >NUL REG add "%%K" /v StateFlags0000 /t REG_DWORD /d 2 /f cleanmgr /sagerun:0
  3. Save the file with a .bat extension, for example, cleanup.bat.
  4. Now, right-click the batch file and select Run as administrator.

This script automatically sets all cleanup options and runs the Disk Cleanup tool without any user interaction, providing a fully automated solution.

4. Scheduling Disk Cleanup with Task Scheduler

  1. Press the Windows key, type Task Scheduler, and open it.
  2. Create a New Task:
    • In the Actions pane, click Create Basic Task….
    • Name your task and click Next.
    • Configure the task to run at your desired schedule (daily, weekly, etc).
    • In the Start a Program section, input cleanmgr.exe in the Program/script box and /SAGERUN:1 in the Add arguments box.

Scheduling Disk Cleanup with Task Scheduler ensures regular maintenance of disk space without manual initiation.

5. Using PowerShell for enhanced control

  1. Press the Windows key, type PowerShell and select Run as administrator from the results.
  2. Run the PowerShell Script:
    • Use the following script: $volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches" foreach ($key in $volumeCaches) { New-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0000 -Value 2 -Type DWORD -Force | Out-Null } Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:0"
    • Save the script as cleanup.ps1 and execute it.

This PowerShell script gives you finer control over the cleanup process and can be easily integrated into larger automation frameworks.

6. Automate the Cleanup task using Group Policy

  1. Press the Windows key, type gpmc.msc, and open Group Policy Management Console.
  2. Create or Edit a Group Policy Object (GPO):
    • Navigate to the target OU (Organizational Unit).
    • Right-click and select Create a GPO in this domain, and Link it here….
    • Name your GPO and click OK.
  3. Configure the GPO:
    • Right-click on the new GPO and select Edit.
    • Navigate to Computer Configuration > Policies > Windows Settings -> Scripts (Startup/Shutdown).
    • Right-click on Startup, select Properties, then Add.
    • Browse to your cleanup scripts such as cleanup.bat or cleanup.ps1 you’ve created in the previous solutions and add them.

Using Group Policy allows you to deploy the automated cleanup task across multiple machines in a domain environment, ensuring consistent disk space management.

By following these steps, you can automate the Disk Cleanup process in Windows, ensuring regular and efficient maintenance of your system’s storage without user intervention. Regularly check your automated tasks to ensure they run smoothly and adjust settings as needed to keep your system optimized.

While you’re at this, you might get extra info from our Windows 11 Disk Cleanup guide. Also, you can follow the highlighted guide if Disk Cleanup is missing.

For any suggestions or questions, hit the comments section below and let us know about them.

More about the topics: Disk Cleanup