How to clean computer using cmd [command prompt]

Author: Rupam Bharali

Updated On:

Microsoft Windows commonly referred to as windows are one of, if not the most popular operating systems for personal and business computers. No doubt you will be able to work on a massive amount of features on your PC. 

With a lot of background tasks going on, PC memory needs to be cleaned up from time to time. Windows operating system allows you to clean your PC memory in many different ways. You can use third-party software to flash temporary files on HDD, or you can use windows inbuilt disk cleanup facilities on the command prompt.

In this post, we will mainly look at how you can clean computer using cmd (command prompt). 

  1. Type “cmd” on the windows search bar / run the dialog box.
  2. Right-click on Command Prompt.
  3. Select Run as administrator.
  4. You will arrive at the command prompt window –
clean-computer-using-cmd
tutorial: clean computer using cmd

Tip: When you run cmd.exe as an administrator, you will have elevated permission with your commands,, which will be operated from the system32 directory as the currently logged-in user.

Heading to the cleaning commands, you have several commands to clean your PC when it comes to cleaning your hard disk or cache memory. To flash remaining unwanted files, you will be able to clean them manually by clearing your temp folder. You can boost the cleaning process by accessing Disk Defragmenter, diskpart, and the Disk Cleanup tool with commands.

Later in this post, you will find the list of commands to clear different parts of your PC memory and the steps to execute it.

Why Clean Up Your Computer Using Command Prompt?

It’s obvious that the GUI applications are effortless to use. But using a Windows command processor is fun and really powerful. If you are among those, who do not fancy loading additional tools on their PC memory and stick to the command prompt for its efficiency & performance benefits, then this guide covers it all.

Cleaning your PC memory is as important as cleaning up the hardware. While running programs, your computer generates temporary files which become unnecessary after use. Though these files are tiny, the collection of such files can cover many Gigabytes and thus has to be cleared often. Clean hardware helps your PC to run smoothly and extend the PC’s lifespan, whereas software & memory cleanup saves your applications from crashing, and possible conflicts, and provides better memory performance.

Cleaning a disk can keep your PC fresh and terminate the possibility of lagging or slowing down. With powerful hardware nowadays, it won’t trouble you but flushing unwanted files from a hard disk is a good idea. 

What is the Command Prompt?

Cmd is a default command-line interpreter for the operating system such as windows, Linux, ArcaOS, eComStation, etc. Command Prompts is a text-based interface within the operating system. It offers easy access to any directory or file manually. It is used to solve troubleshoot issues and any kind of windows issues. Some command prompts also work as disk cleanup managers.

Commands to clean computer using Command Prompt

The following part of this post contains the list of commands you can use to clean a computer using cmd. Before using these commands, make sure to understand what they do and only perform them if necessary. 

1. Clean unwanted files using Cleanmgr command class

This class of commands allows you to clean unwanted files and free up disk space on a computer’s hard drive. It will first analyze the files on your computer that are no longer of any use, then remove those files. It has sub-commands dedicated to specific functions.

1.1 Cleanmgr

Cleanmgr is a disk cleanup command line to clean junk. Follow these steps to use this command.

  • Cleanmgr is a disk cleanup command line to clean junk.
  • To use it, open the command prompt and type “Cleanmgr.”
  • A small window will appear where you have to select the drive to clean. 
  • Click on OK to ask your system to analyze the drive and find out removable junks.
  • Check which files you want to delete and press the OK button to execute.
how-to-clean-computer-using-cmd
Tutorial: clean computer using cmd – cleanmgr command

If you want to automate the disk cleanup task, then there are three command-line switches that Cleanmgr accepts. They are /sageset, /sagerun and /lowdisk.

1.2 Cleanmgr/sageset

This command will skip the disk selection step and take you straight to the category selection menu, where you need to check which category of junk files you want to clean.

The format of running the sageset command is;

cleanmgr/ sageset: numeric_id  

for example:

c:\Windows\system32> cleanmgr/ sageset: 1

1.3 Cleanmgr/ sagerun

This command will automate the disk-cleaning process without selecting the categories you need to use the sagerun command. 

The format of running the sagerun command is;

cleanmgr/ sagerun: id

for example:

c:\Windows\system32> cleanmgr/ sagerun: 1

1.4 Cleanmgr/lowdisk

This command is used when the disk drive is running in low disk space. When this switch is run, the disk cleanup opens checked with all checkboxes by default. The format of running the lowdisk command is;

cleanmgr/lowdisk/d{driver-latter of your disk}

for example:

c:\Windows\system32> cleanmgr/lowdisk/dc {for c drive}
c:\Windows\system32> cleanmgr/lowdisk/dd {for d drive}

Tip: these commands work on server 2003, server 2008, Windows XP, Vista, Windows 7, 8, and Windows 10.

2. Clean temporary files using cleanup command

Temporary files are referred to as Temp. These files store info for a transient period during the execution of an application, as well as they can be created for backup purposes. Primarily they are used to store, move data and recover lost data. 

It is safe to delete temporary files if and only if they are not being used by any application. Usually, Windows will prevent you from deleting these files during run time can result in application task failure.

The temporary files(Temp) created by OS are stored in %system%/windows/temp while temporary files (%Temp%) created by the users are stored in C:\Users\username\AppData\Local\Temp.

To view temp files using command prompt use: 

%SystemRoot%\explorer.exe %temp%\

And the command to delete temp files (you can manually delete them too from the directory):

del %temp%\*.*/s/q
delete-temp-files
Tutorial: clean computer using cmd – delete temp files

3. Clean prefetch files using command prompt

Windows creates all sorts of temporary files on your hard drive. Prefetch files are created by the operating system when an application is used for the first time and later works as a cache. They may cover lots of space based on how the application tends to use the dedicated file.

Prefetch is basically to make the app faster, but if you are facing app bugs or lags, you can delete these files.

To check the prefetch files on your computer simply check in the prefetch folder, like this (on cmd):

%SystemRoot%\explorer.exe C:\Windows\prefetch\

And then delete the files from the prefetch folder;

del C:\Windows\prefetch\*.*/s/q
delete-prefetch-using-cmd
Tutorial: clean computer using cmd – clean prefetch

Similar content: Tutorial to Remove Virus Using CMD in 4 Steps

4. Clear cache memory in windows

Cache files are helpful for application performance, but, If there’s something wrong with cache files, it can cause data fetching issues, glitches, or even crash. That’s why a common remedy is to clear application cache memory from time to time.

You can delete the DNS cache, Windows store cache from your PC without any hesitation. If there’s an issue with deleting these cache files, windows will notify you. To flush the DNS cache, start command prompt as administrator. 

  1. Search for the command prompt on the windows search bar.
  2. Right-click on the command prompt and select Run as administrator.
  3. Next in the command prompt run this command –
ipconfig/flushDNS

You will receive a message that lets you know the DNS cache is successfully flushed.

And to clear windows store cache memory, you will need to use the WSReset command in the cmd. Doing so will display a black screen like the image below, and all you need to do is wait until the windows disappear to be sure that windows store memory is successfully flushed.

windows-store-reset-command
clean computer using cmd – clean windows store cache

5. Defragment your hard disk with command prompt

Another part of cleaning computer memory is defragmenting your disks.

Defragmenting is the process of reorganizing the data stored in a hard drive. It is a process that reduces the degree of fragmentation by physically organizing the content of mass storage into the smallest number of adjacent regions. Reordering the files will allow your PC to access files faster from the hard disk.

Disk fragmentation is a natural occurrence on traditional hard disks, although it is vulnerable to PC performance. Basically, fragments affect the writing and accessing speed of your HDD, resulting in sluggish system performance.

To defragment your hard drive using cmd;

  1. Start the command prompt as administrator.
  2. Use defrag {preferred drive’s drive later}: defrag c: 

Note that c: in the defrag c: command is the drive letter that means the drive you want to defrag. Change the drive letter if you want to defrag another drive.

Defragment-Drive-C
clean computer using cmd – defragment your disks

7. Use Diskpart command to erase data from HDD

Diskpart is a command-line disk partitioning utility that is available in Windows 2000 and later editions. Previously Fdisk command was used for this task. You can use the diskpart command line to clean your disk along with specific switches like “clean” & “clean all” to erase data from the hard drive. 

To clean hard drives with the diskpart command follow the steps below;

  1. Start the command prompt as administrator.
  2. Type diskpart command and press enter. 
  3. Following the diskpart, type “list disk” in cmd.
  4. It will display all connected disks to your computer along with their availability status.
  5. The type select disk n {for example: select disk 0} 
  6. ** if the disk status is offline, type “online disk” and press enter.
  7. Then type “clean” or “clean all” according to your need.
  8. On success, you will see the “diskpart succeeded in cleaning the disk” message.

Clean command will only clean data on HDD and will be marked as deleted. However, the clean all command will delete every byte of data from the disk, and later you won’t be able to recover that data.

clean-diskpart
clean computer using cmd – diskpart command

👉🏽  Related article: Best Ryzen 5 5600H Laptops Under 85k INR (November 2021)

8. DISM Windows image cleanup

If you are having trouble with the current operating system, then you can repair the issue with OS. It’s an image cleaning task that will restore the operating system’s health to its ideal state.

The command line you will use here is known as DISM (deployment image servicing and management), which is a built-in command line used to manage windows image (.wim) and virtual hard disk (.vhd). It is also used in the windows recovery environment and windows setup process, as well as in updating the operating system.

To check if there is any corruption, follow the steps below; Start cmd with admin privilege.

Start cmd with admin privilege > Type this command.

DISM /online /Cleanup-Image /checkhealth
dism-checkhealth
clean your computer using cmd – check OS health

And to scan the windows image use this command;

DISM /Online /Cleanup-Image /ScanHealth
dism-scanhealth
Clear your computer using cmd – scan health command

To fix the windows images, If you find any corrupted files use this command;

DISM /Online /Cleanup-Image /RestoreHealth /Source:repairSource\install.wim

You can scan your PC for malware/viruses using the command prompt. The scanning process will be performed by Windows defender.

Wrapping up: A command prompt is a powerful tool; you can it for many other purposes on your PC. If you have more info regarding PC clean commands, let us know in the comment below.

Similar articles:

  1. 10 Unique iPhone 11 Tips and Tricks You Must Try
  2. Top 5 Free Noise Cancellation Software You Must Try
  3. Top 10 Best RPG games for android you must try
  4. 10 Best Sword Enchantments In Minecraft [2022]
Photo of author

Rupam Bharali

Hey there, I'm Rupam. I like gaming, exploring gadgets & software, watching TV shows and other people's streams. I created this website to share my thoughts on people's queries regarding software-related issues, guides, streaming, gaming, and PCs. I hope you find my content helpful. Feel free to take a glance and share your thoughts with me.

2 thoughts on “How to clean computer using cmd [command prompt]”

  1. You don’t have to defrag an SSD. If you have done it a few times, it isn’t going to cause you much trouble or harm your SSD. You just don’t want this to be a scheduled, weekly type thing that takes away from the finite number of SSD rewrites.

Comments are closed.