How to remove file in cmd Windows?

Sometimes you may need to delete a file from the command line in Windows. Whether you want to remove a temporary file, delete an old unneeded file, or automate file removal, using the command line can make deleting files quick and easy.

The main command for removing files in the Windows command line is the ‘del’ command. This allows you to delete one or more files by specifying their names. Here’s a quick overview of how to use the del command to remove files from the cmd prompt in Windows:

Quick Steps

  • Open the command prompt window
  • Navigate to the folder containing the file you want to delete
  • Type ‘del filename’ and hit Enter to delete the file
  • Or type ‘del *.*’ to delete all files in the current folder

In this article, we’ll go into more detail on how to use the del command and its options to remove files through the Windows command line interface.

Open the Command Prompt

The first step to delete a file from cmd is to open the command prompt window. This is the interface where you can enter text-based commands.

Here are a few ways to open the command prompt in Windows:

From the Start Menu

  • Click the Windows Start menu and search for ‘cmd’
  • Select the Command Prompt app to open it

With Run

  • Press Windows + R to open the Run dialog box
  • Type ‘cmd’ and hit Enter

From File Explorer

  • Open any folder in File Explorer
  • Type ‘cmd’ in the address bar and press Enter
  • This will open the command prompt at that folder location

Once you have the command prompt window open, you can start using commands like the del command to remove files and folders.

Navigate to the File Location

Before you can delete a file in cmd, you first need to navigate to the folder containing that file.

The del command will only remove files that are in the current working directory that you have changed to in the command prompt.

Here are some basic navigation commands:

Change Folder

Use the ‘cd’ command to change directories. For example:

cd C:\Users\MyUser\Documents

This would move you into the Documents folder.

List Files

List the files and folders in the current directory with ‘dir’:

dir

This displays the contents so you can ensure you are in the correct location.

Move Up a Folder

Use ‘cd ..’ to go up one folder level:

cd ..

So if you were in C:\Users\MyUser\Documents, this would take you back to C:\Users\MyUser.

Use these navigation commands to get to the folder containing the file you want to delete before using the del command.

Delete a Single File

Once you’re in the right folder, deleting a single file is easy.

Simply type:

del filename

For example:

del document.txt

This will immediately remove the file named ‘document.txt’ from the current folder.

You will be asked to confirm the deletion. Type ‘y’ and hit Enter to confirm that you want to remove the file permanently.

The file will then be deleted and you’ll return to the prompt.

Example

“`
C:\Users\MyUser\Documents> del document.txt
Are you sure (Y/N)? y

C:\Users\MyUser\Documents>
“`

And the file ‘document.txt’ would be removed from that Documents folder.

Delete Multiple Files

You can also use the del command to delete multiple files at once.

To delete a group of files, list each filename separated by a space:

del file1.txt file2.txt file3.txt

This will delete all three files in one command.

Wildcards

You can also use wildcards like * to delete files matching a pattern.

For example:

del *.tmp

This will delete all files ending in .tmp in the current folder.

Or:

del *.*

This special syntax will delete all files in the folder.

So with the del command and wildcards, you can quickly delete multiple files matching different patterns.

Delete Folders

In addition to files, you can also use the /S switch to delete entire folders and their contents.

For example:

del /S foldername

This will remove the folder and all files and subfolders inside it.

Again, you will need to confirm the deletion.

Be very careful with /S as it can recursively delete the contents of an entire drive if you’re not specific enough with the folder path!

Example

“`
C:\Users\MyUser\Documents> del /S tempfolder
tempfolder, Are you sure (Y/N)? y
“`

This deletes tempfolder and everything inside it.

Delete Files Older Than X Days

One useful option is using the /P switch to delete files older than a certain number of days.

For example:

del /P 60 *.*

This will prompt you to delete all files that are over 60 days old in the current folder.

You can change the number to anything you want to delete old files older than X days.

Example

“`
C:\Users\MyUser\Downloads> del /P 7 *.*
Would you like to delete all files older than 7 days in this folder? (Y/N) y
“`

This prompts you to delete any file older than 7 days in Downloads. Great for cleaning out old downloads.

Additional Options

Here are some other useful options and switches for the del command:

/F

Force delete of read-only files.

del /F filename

/Q

Quiet mode, don’t ask for confirmation when deleting files.

del /Q *.*

/A

Select files to delete based on attributes like read-only, archived, system, and more.

For example, to delete read-only files:

del /A R *.*

See ‘del /?’ for a full list of attributes.

Delete File Shortcuts

One thing to note is that using the del command will only remove the actual file, not any shortcuts or aliases that point to that file.

So if you have a shortcut on your desktop to a file and delete the original file in cmd, the desktop shortcut will remain but point to nothing.

To also remove shortcuts, you need to delete those manually or use a wildcard like ‘*.*’ to delete shortcuts alongside actual files.

Conclusion

In summary, here are the key points about using the del command to delete files from the Windows command prompt:

  • Open the command prompt window
  • Navigate to the folder containing the file
  • Use ‘del filename’ to delete a specific file
  • Add wildcards like * to delete multiple files
  • Use switches like /S to delete folders
  • Include /P to remove old files over X days old
  • Add options like /F to force delete read only files
  • Remember shortcuts may need to be deleted separately

With the del command and its various switches, you have a powerful tool to delete files and folders right from the command line. It’s much faster than using your mouse and GUI for deleting multiple or old files.

Just be cautious when using del with wildcards or recursive deletes as you can accidentally remove important files if you’re not careful. But with proper care, del gives you precise control over mass file deletion from the cmd prompt in Windows.

More Examples

Here are some more examples of using the del command in different ways:

Delete a single file by name:

del report.docx

Delete all .log files in a folder:

del *.log

Delete all contents of a folder recursively:

del /S tempfolder

Delete system and hidden files in the current folder:

del /A S /A H *.*

Delete files modified over 90 days ago:

del /P 90 *.*

Force delete read-only files without prompting:

del /Q /F *.bak

The del command is a simple but powerful tool for file deletion from the Windows command line. With just a few commands, you can precisely control the removal of multiple files based on name, date, attributes and more. Next time you need to do some bulk deleting, open cmd and use del for a quick, automated and efficient way to remove files.

Command Description
del filename Delete a single file
del *.txt Delete files matching a wildcard pattern
del /S folder Recursively delete a folder and its contents
del /P days *.* Delete files older than X days
del /F file.bak Force delete read-only files
del /Q /A H *.* Quickly delete hidden files without prompting