Command delete file windows command line

Find syntax and examples for windows delete command. Learn how to delete files in different scenarios using this command.

on August 5, 2015

Deleting files is one of the frequently done operation from Windows command prompt. This post explains how to use ‘del’ command from CMD for different use cases like deleting a single file, deleting files in bulk using wild cards etc. Before we start to look at the syntax, note that the command works only for files and can’t handle folders.

How to delete a file

Run del command with the name of the file to be deleted, you are done!

del filename

You do not see message after running the command if the file is deleted successfully. Error message is shown only when something goes wrong.

Delete files in bulk

Del command recognizes wildcard(*) and so can be used to delete files in bulk from CMD.  Some examples below.
To delete all the files in current folder

del *

To delete all the files with ‘log’ extension

del *.log

Delete all files having the prefix ‘abc’

del abc*

Delete all files having ‘PIC’ somewhere in the file name.

del *PIC*

The above are the basic use cases of del command. Continue to read below for non trivial use cases.

Delete multiple files

‘Del’ command can accept multiple files as argument

del filename1 filename2 filename3 filename4....

Example:

D:>dir /s /b
1.pdf 2.pdf 3.pdf
D:>del 1.pdf 2.pdf 3.pdf
D:>
D:>dir /s /b
D:>

Delete Read only files

We can’t delete a read-only file using simple‘del’ command. We get access denied error in this scenario.

c:>attrib readonlyfile.txt
A    R       C:readonlyfile.txt
c:>del readonlyfile.txt
c:readonlyfile.txt Access is denied.
c:>

A read-only file can be deleted by adding /F flag.

del /F readonlyfile.txt

Alternatively, we can use the below command too

del /A:R readonlyfile.txt

cmd Delete Folder – How to Remove Files and Folders in Windows

Sometimes it’s just faster to do things with the command line.

In this quick tutorial we’ll go over how to open Command Prompt, some basic commands and flags, and how to delete files and folders in Command Prompt.

If you’re already familiar with basic DOS commands, feel free to skip ahead.

How to open Command Prompt

To open Command Prompt, press the Windows key, and type in «cmd».

Then, click on «Run as Administrator»:

Screenshot showing how to open Command Prompt as an administrator

After that, you’ll see a Command Prompt window with administrative privileges:

command-prompt-new-window

Screenshot of Command Prompt window

If you can’t open Command Prompt as an administrator, no worries. You can open a normal Command Prompt window by clicking «Open» instead of «Run as Administrator».

The only difference is that you may not be able to delete some protected files, which shouldn’t be a problem in most cases.

How to delete files with the del command

Now that Command Prompt is open, use cd to change directories to where your files are.

I’ve prepared a directory on the desktop called Test Folder. You can use the command tree /f to see a, well, tree, of all the nested files and folders:

Screenshot after running tree /f in target directory

To delete a file, use the following command: del "<filename>".

For example, to delete Test file.txt, just run del "Test File.txt".

There may be a prompt asking if you want to delete the file. If so, type «y» and hit enter.

Note: Any files deleted with the del command cannot be recovered. Be very careful where and how you use this command.

After that, you can run tree /f to confirm that your file was deleted:

Screenshot after deleting file with del command

Also, bonus tip – Command Prompt has basic autocompletion. So you could just type in del test, press the tab key, and Command Prompt will change it to del "Test File.txt".

How to force delete files with the del command

Sometimes files are marked as read only, and you’ll see the following error when you try to use the del command:

Screenshot of error after trying to delete a read only file

To get around this, use the /f flag to force delete the file. For example, del /f "Read Only Test File.txt":

Screenshot after deleting file with the force flag

How to delete folders with the rmdir command

To delete directories/folders, you’ll need to use the rmdir or rd command. Both commands work the same way, but let’s stick with rmdir since it’s a bit more expressive.

Also, I’ll use the terms directory and folder interchangeably for the rest of the tutorial. «Folder» is a newer term that became popular with early desktop GUIs, but folder and directory basically mean the same thing.

To remove a directory, just use the command rmdir <directory name>.

Note: Any directories deleted with the rmdir command cannot be recovered. Be very careful where and how you use this command.

In this case I want to remove a directory named Subfolder, so I’ll use the command rmdir Subfolder:

Screenshot of a directory not empty error

But, if you remember earlier, Subfolder has a file in it named Nested Test File.

You could cd into the Subfolder directory and remove the file, then come back with cd .. and run the rmdir Subfolder command again, but that would get tedious. And just imagine if there were a bunch of other nested files and directories!

Like with the del command, there’s a helpful flag we can use to make things much faster and easier.

How to use the /s flag with rmdir

To remove a directory, including all nested files and subdirectories, just use the /s flag:

Screenshot after running rmdir with the /s flag

There will probably be a prompt asking if you want to remove that directory. If so, just type «y» and hit enter.

And that’s it! That should be everything you need to know to remove files and folders in the Windows Command Prompt.

All of these commands should work in PowerShell, which is basically Command Prompt version 2.0. Also, PowerShell has a bunch of cool aliases like ls and clear that should feel right at home if you’re familiar with the Mac/Linux command line.

Did these commands help you? Are there any other commands that you find useful? Either way, let me know over on Twitter.



Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

None of the answers as posted on 2018-06-01, with the exception of the single command line posted by foxidrive, really deletes all files and all folders/directories in %PathToFolder%. That’s the reason for posting one more answer with a very simple single command line to delete all files and subfolders of a folder as well as a batch file with a more complex solution explaining why all other answers as posted on 2018-06-01 using DEL and FOR with RD failed to clean up a folder completely.


The simple single command line solution which of course can be also used in a batch file:

pushd "%PathToFolder%" 2>nul && ( rd /Q /S "%PathToFolder%" 2>nul & popd )

This command line contains three commands executed one after the other.

The first command PUSHD pushes current directory path on stack and next makes %PathToFolder% the current directory for running command process.

This works also for UNC paths by default because of command extensions are enabled by default and in this case PUSHD creates a temporary drive letter that points to that specified network resource and then changes the current drive and directory, using the newly defined drive letter.

PUSHD outputs following error message to handle STDERR if the specified directory does not exist at all:

The system cannot find the path specified.

This error message is suppressed by redirecting it with 2>nul to device NUL.

The next command RD is executed only if changing current directory for current command process to specified directory was successful, i.e. the specified directory exists at all.

The command RD with the options /Q and /S removes a directory quietly with all subdirectories even if the specified directory contains files or folders with hidden attribute or with read-only attribute set. The system attribute does never prevent deletion of a file or folder.

Not deleted are:

  1. Folders used as the current directory for any running process. The entire folder tree to such a folder cannot be deleted if a folder is used as the current directory for any running process.

  2. Files currently opened by any running process with file access permissions set on file open to prevent deletion of the file while opened by the running application/process. Such an opened file prevents also the deletion of entire folder tree to the opened file.

  3. Files/folders on which the current user has not the required (NTFS) permissions to delete the file/folder which prevents also the deletion of the folder tree to this file/folder.

The first reason for not deleting a folder is used by this command line to delete all files and subfolders of the specified folder, but not the folder itself. The folder is made temporarily the current directory for running command process which prevents the deletion of the folder itself. Of course this results in output of an error message by command RD:

The process cannot access the file because it is being used by another process.

File is the wrong term here as in reality the folder is being used by another process, the current command process which executed command RD. Well, in reality a folder is for the file system a special file with file attribute directory which explains this error message. But I don’t want to go too deep into file system management.

This error message, like all other error messages, which could occur because of the three reasons written above, is suppressed by redirecting it with 2>nul from handle STDERR to device NUL.

The third command, POPD, is executed independently of the exit value of command RD.

POPD pops the directory path pushed by PUSHD from the stack and changes the current directory for running the command process to this directory, i.e. restores the initial current directory. POPD deletes the temporary drive letter created by PUSHD in case of a UNC folder path.

Note: POPD can silently fail to restore the initial current directory in case of the initial current directory was a subdirectory of the directory to clean which does not exist anymore. In this special case %PathToFolder% remains the current directory. So it is advisable to run the command line above not from a subdirectory of %PathToFolder%.

One more interesting fact:
I tried the command line also using a UNC path by sharing local directory C:Temp with share name Temp and using UNC path \%COMPUTERNAME%TempCleanTest assigned to environment variable PathToFolder on Windows 7. If the current directory on running the command line is a subdirectory of a shared local folder accessed using UNC path, i.e. C:TempCleanTestSubfolder1, Subfolder1 is deleted by RD, and next POPD fails silently in making C:TempCleanTestSubfolder1 again the current directory resulting in Z:CleanTest remaining as the current directory for the running command process. So in this very, very special case the temporary drive letter remains until the current directory is changed for example with cd /D %SystemRoot% to a local directory really existing. Unfortunately POPD does not exit with a value greater 0 if it fails to restore the initial current directory making it impossible to detect this very special error condition using just the exit code of POPD. However, it can be supposed that nobody ever runs into this very special error case as UNC paths are usually not used for accessing local files and folders.

For understanding the used commands even better, open a command prompt window, execute there the following commands, and read the help displayed for each command very carefully.

  • pushd /?
  • popd /?
  • rd /?

Single line with multiple commands using Windows batch file explains the operators && and & used here.


Next let us look on the batch file solution using the command DEL to delete files in %PathToFolder% and FOR and RD to delete the subfolders in %PathToFolder%.

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Clean the folder for temporary files if environment variable
rem PathToFolder is not defined already outside this batch file.
if not defined PathToFolder set "PathToFolder=%TEMP%"

rem Remove all double quotes from folder path.
set "PathToFolder=%PathToFolder:"=%"

rem Did the folder path consist only of double quotes?
if not defined PathToFolder goto EndCleanFolder

rem Remove a backslash at end of folder path.
if "%PathToFolder:~-1%" == "" set "PathToFolder=%PathToFolder:~0,-1%"

rem Did the folder path consist only of a backslash (with one or more double quotes)?
if not defined PathToFolder goto EndCleanFolder

rem Delete all files in specified folder including files with hidden
rem or read-only attribute set, except the files currently opened by
rem a running process which prevents deletion of the file while being
rem opened by the application, or on which the current user has not
rem the required permissions to delete the file.
del /A /F /Q "%PathToFolder%*" >nul 2>nul

rem Delete all subfolders in specified folder including those with hidden
rem attribute set recursive with all files and subfolders, except folders
rem being the current directory of any running process which prevents the
rem deletion of the folder and all folders above, folders containing a file
rem opened by the application which prevents deletion of the file and the
rem entire folder structure to this file, or on which the current user has
rem not the required permissions to delete a folder or file in folder tree
rem to delete.
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%%%I" 2>nul

:EndCleanFolder
endlocal

The batch file first makes sure that environment variable PathToFolder is really defined with a folder path without double quotes and without a backslash at the end. The backslash at the end would not be a problem, but double quotes in a folder path could be problematic because of the value of PathToFolder is concatenated with other strings during batch file execution.

Important are the two lines:

del /A /F /Q "%PathToFolder%*" >nul 2>nul
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%%%I" 2>nul

The command DEL is used to delete all files in the specified directory.

  • The option /A is necessary to process really all files including files with the hidden attribute which DEL would ignore without using option /A.
  • The option /F is necessary to force deletion of files with the read-only attribute set.
  • The option /Q is necessary to run a quiet deletion of multiple files without prompting the user if multiple files should be really deleted.
  • >nul is necessary to redirect the output of the file names written to handle STDOUT to device NUL of which can’t be deleted because of a file is currently opened or user has no permission to delete the file.
  • 2>nul is necessary to redirect the error message output for each file which can’t be deleted from handle STDERR to device NUL.

The commands FOR and RD are used to remove all subdirectories in specified directory. But for /D is not used because of FOR is ignoring in this case subdirectories with the hidden attribute set. For that reason for /F is used to run the following command line in a separate command process started in the background with %ComSpec% /c:

dir "%PathToFolder%*" /AD /B 2>nul

DIR outputs in bare format because of /B the directory entries with attribute D, i.e. the names of all subdirectories in specified directory independent on other attributes like the hidden attribute without a path. 2>nul is used to redirect the error message output by DIR on no directory found from handle STDERR to device NUL.

The redirection operator > must be escaped with the caret character, ^, on the FOR command line to be interpreted as a literal character when the Windows command interpreter processes this command line before executing the command FOR which executes the embedded dir command line in a separate command process started in the background.

FOR processes the captured output written to handle STDOUT of a started command process which are the names of the subdirectories without path and never enclosed in double quotes.

FOR with option /F ignores empty lines which don’t occur here as DIR with option /B does not output empty lines.

FOR would also ignore lines starting with a semicolon which is the default end of line character. A directory name can start with a semicolon. For that reason eol=| is used to define the vertical bar character as the end-of-line character which no directory or file can have in its name.

FOR would split up the line into substrings using space and horizontal tab as delimiters and would assign only the first space/tab delimited string to specified loop variable I. This splitting behavior is not wanted here because of a directory name can contain one or more spaces. Therefore delims= is used to define an empty list of delimiters to disable the line splitting behavior and get assigned to the loop variable, I, always the complete directory name.

Command FOR runs the command RD for each directory name without a path which is the reason why on the RD command line the folder path must be specified once again which is concatenated with the subfolder name.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?
  1. Image titled Delete a File Using Command Prompt Step 1

    1

    Locate your file. If you know where the file is, you can navigate to it by simply opening the appropriate folder. For example, if you’re trying to delete a picture or a text file, you will likely want to look in the default «Documents» folder, which typically holds those file types.

    • If you don’t know where your file is, type its name into the Start search bar, right-click the file when it pops up, and click Open file location to go directly to the file.
  2. Image titled Delete a File Using Command Prompt Step 2

    2

    Click and drag your file onto the desktop. Doing so will make the deletion process easier as you won’t have to change the deletion location from within Command Prompt.

    • The exception to this rule is if you’re trying to delete a file from the «System32» folder, which is Windows’ system files folder. If that’s the case, leave your file there.

    Advertisement

  3. Image titled Delete a File Using Command Prompt Step 3

    3

    Right-click your file. This will prompt a drop-down menu.

  4. Image titled Delete a File Using Command Prompt Step 4

    4

    Click Properties. It’s at the bottom of the drop-down menu.

  5. Image titled Delete a File Using Command Prompt Step 5

    5

    Look at the file extension. The file’s extension is listed near the top of the «General» tab in the «Properties» window, to the right of the «Type of file:» text. You’ll need to know your file’s extension in order to delete it using Command Prompt. Common extensions include the following:

    • .txt — Text files (files made in Notepad).
    • .docx — Microsoft Word files.
    • .jpg or .png — Picture files.
    • .mov, .wmv, .mp4 — Video files.
    • .mp3, .wav — Sound files.
    • .exe — Executable files (e.g., a setup file).
    • .lnk — Shortcut files. Deleting a shortcut will not remove the attached program from your computer.
  6. Image titled Delete a File Using Command Prompt Step 6

    6

    Write down the file extension. Once you know the file extension, you’re ready to open and use Command Prompt.

  7. Advertisement

  1. Image titled Delete a File Using Command Prompt Step 7

    1

    Open Command Prompt. In this case, you’ll want to avoid the «Administrator» (or «Admin») version of Command Prompt unless you’re deleting a file in the «System32» folder. You can open Command Prompt in a variety of ways depending on your version of Windows:

    • Hold down Win and press X, then click Command Prompt above the Start button.
    • Right-click the Start button in the bottom-left corner of the screen, then click Command Prompt in the pop-up window.
    • Type «Command Prompt» into the Start menu search bar (for Windows 8, hover your mouse in the top-right corner of the screen and click the magnifying glass), then click the «Command Prompt» icon when it appears.
    • Open the «Run» app from the Start menu, type in «cmd», and click OK.
  2. Image titled Delete a File Using Command Prompt Step 8

    2

    Type in cd desktop and press Enter. Doing so will change the location (or «directory») in Command Prompt to your desktop.

    • There are other ways you can change the Command Prompt directory if need be.
    • Opening Command Prompt in «Administrator» mode will change the directory to the «System32» file. For this reason, do not open Command Prompt in «Administrator» unless your file is in the «System32» folder.
  3. Image titled Delete a File Using Command Prompt Step 9

    3

    Type in del [filename.filetype]. Replace «filename.filetype» with your file’s actual name and extension.

    • For example, a picture file named «icecream» would become icecream.png, a text file named «notes» would become notes.txt, and so on.
    • For files with spaces in their names, place quotation marks around the entire file name: "I like turtles.jpg" instead of I_like_turtles.jpg or similar.
    • To delete all files on your desktop that share the same extension (e.g., all text files), type *.filetype where «filetype» is the extension (e.g., *.txt).
  4. Image titled Delete a File Using Command Prompt Step 10

    4

    Press Enter. You will see a new, blank line appear in Command Prompt. Your file is now gone.

    • Since the «del» command removes files directly from your hard drive, you won’t need to delete the file again from the Recycling Bin.
  5. Advertisement

Add New Question

  • Question

    What is the extension type of a folder?

    Community Answer

    Only files have extensions. Directories are handled differently by the filesystem, and thus do not need extensions.

  • Question

    Some type of virus installed to my PC and won’t let me use Command Prompt. What should I do?

    Community Answer

    Try running an antivirus program to eliminate the virus. If that doesn’t work, you’ll probably need to take your computer in to a tech department somewhere (e.g., Best Buy) and have them take a look at removing the virus.

  • Question

    When I typed «cd desktop» in Command Prompt’s Administrative mode, it shows «the system cannot find the path specified». What should I do?

    Community Answer

    You should open Command Prompt in its non-Admin format—e.g., «Command Prompt» instead of «Command Prompt (Admin)». Since Command Prompt (Admin) opens inside of the «System32» folder, you won’t be able to open the desktop from there (because the «Desktop» folder is under your name in the «Users» folder, not in the «System 32» folder).

See more answers

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

  • It is recommended that you use your system’s file manager to delete files and only use Command Prompt when it is needed to delete files that require a more forceful approach.

Thanks for submitting a tip for review!

Advertisement

  • If you delete a system file, your computer might cease to work.

  • Using Command Prompt to delete files will entirely bypass the Recycling Bin.

Advertisement

About This Article

Article SummaryX

1. Right-click the Start menu.
2. Click Command Prompt.
3. Type «cd FOLDERTHATCONTAINSFILE» and press Enter.
4. Type «del filename» and press Enter.

Did this summary help you?

Thanks to all authors for creating a page that has been read 982,679 times.

Is this article up to date?

Some folders and files are impossible to delete using Windows Explorer. These include files with long paths, names or reserved names like CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL etc. You will get an Access Denied error message when you try to delete these files using Windows Explorer, even if you are an administrator.

Regardless of the reason, these can only be force deleted using command line only. This article explains using cmd to delete folder or file successfully.

Table of contents

  • Before we begin
  • How to remove files and folders using Command Prompt
    • Del/Erase command in cmd
    • Rmdir /rd command in cmd
    • Delete multiple files and folders
    • Delete files and folders in any directory
    • Check the existence of file or folder then remove using IF command
  • How to remove files and folders using Windows PowerShell
    • Delete multiple files and folders
    • Delete files and folders in any directory
  • Delete files and folders with complex and long paths using the command line
  • Closing words

Before we begin

Here are some important things for you to understand before we dig into removing files and folders using Command Prompt and Windows PowerShell. These tips will help you understand the terms and some basic rules of the commands that will be used further in the article.

The most important thing to remember here is the syntax of the path and file/folder name. When typing file name, notice whether there is a gap (space) in it. For example, if the folder name has no space in it, it can be written as-is. However, if there is a gap in it, it will need to be written within parenthesis (“”). Here is an example:

CLI naming syntax

Another thing to remember is that you might see different outcomes while removing folders that are already empty, and folders that have some content in them. Having said that, you will need to use the dedicated options in the command to remove content from within a folder along with the main folder itself. This is called a recursive action.

Furthermore, you must also know how to change your working directory when inside a Command Line Interface. Use the command cd to change your directory, followed by the correct syntax. Here are some examples:

One last thing that might come in handy is being able to view what content is available in the current working directory. This is especially helpful so that you type in the correct spelling of the target file or folder. To view the contents of the current working directory in Command Prompt and PowerShell, type in Dir.

dir

Now that we have the basic knowledge, let us show you how you can delete files and folders using the command line on a Windows PC.

By default, there are 2 command-line interfaces built into Windows 10 – Command Prompt and Windows PowerShell. Both of these are going to be used‌ ‌to‌ ‌delete‌ ‌content‌ ‌from‌ ‌a‌ ‌computer.

How to remove files and folders using Command Prompt

Let us start with the very basic commands and work our way up from there for Command Prompt. We recommend that you use Command Prompt with administrative privileges so that you do not encounter any additional prompts that you already might have.

Del/Erase command in cmd

Del and Erase commands in Command Prompt are aliases of one another. Meaning, both perform the same function regardless of which one you use. These can be used to remove individual items (files) in the current working directory. Remember that it cannot be used to delete the directories (folders) themselves.

Use either of the following commands to do so:

Tip: Use the Tab button to automatically complete paths and file/folder names.

Del File/FolderName

Erase File/FolderName

Replace File/FolderName with the name of the item you wish to remove. Here is an example of us removing files from the working directory:

del erase cmd

If you try to remove items from within a folder, whether empty or not, you will be prompted for a confirmation action, such as the one below:

confirmation yes no

In such a scenario, you will need to enter Y for yes and N for no to confirm. If you select yes, the items directly within the folder will be removed, but the directory (folder) will remain. However, the subdirectories within the folder will not be changed at all.

This problem can be resolved by using the /s switch. In order to remove all of the content within the folder and its subdirectories, you will need to add the recursive option in the command (/s). The slash followed by “s” signifies the recursive option. Refer to the example below to fully understand the concept:

We will be using the Del command here to recursively remove the text files within the folder “Final folder,” which also has a subdirectory named “Subfolder.” Subfolder also has 2 sample text files that we will be recursively removing with the following command:

Del /s "Final folder"

Here is its output:

recursive del

As you can see in the image above, we had to enter “y” twice – once for each folder. with each confirmation, 2 text files were removed, as we had stated earlier in this example. However, if we use File Explorer, we can still see that both the directories – “Final folder” and “Subfolder” – are still there, but the content inside them is removed.

You can also make another tweak to the command so that it is executed silently and you will not be prompted for confirmation. Here is how:

Del /s /q "Final folder"

The /q illustrates that the action be taken quietly.

quiet del

Rmdir /rd command in cmd

Similar to Del and Erase, rmdir and rd are also aliases for one another, which means to remove directory. These commands are used to remove the entire directory and subdirectories (recursively) including their contents. Use the command below to do so:

rmdir "New Folder"

rmdir cmd

The above command will remove the “New folder” only if it is empty. If a folder has subdirectories, you might get the following prompt:

directory is not empty

In this case, we will need to apply the option for recursive deletion of items as we have done earlier with the Del command.

rmdir /s "Final folder"

rmdir recursive

Of course, this can also be performed with the /q option so that you are not prompted with a confirmation.

rmdir /s /q "Final folder"

rmdir recursive quiet

Delete multiple files and folders

Up until now, we have completed the task of deleting single items per command. Now let’s see how you can remove multiple selective files or folders. Use the command below to do so:

For files:

Del "File1.txt" "File3.txt" "File5.txt"

del multiple files

For directories:

rd "Folder1" "Folder3" "Folder5"

rd multiple folders

Here is a before and after comparison of the directory where both of the above commands were executed:

before vs after

You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command. However, Microsoft has removed the support for the use of asterisks with rmdir so that users do not accidentally remove entire folders.

Here is an example of us removing all .txt files from our current working directory:

Del "*.txt"

del asterisk

Delete files and folders in any directory

We are working on removing content within the current working directory. However, you can also use the commands we have discussed till now to remove files and folders from any directory within your computer.

Simply put the complete path of the item you want to delete in enclosed parenthesis, and it shall be removed, as in the example below:

different directory rmdir

Check the existence of file or folder then remove using IF command

We have already discussed that you can view the contents of the working directory by typing in Dir in Command Prompt. However, you can apply an “if” condition in Command Prompt to remove an item if it exists. If it will not, the action would not be taken. Here is how:

if exist File/FolderName (rmdir /s/q File/FolderName)

Replace File/FolderName in both places with the name of the item (and extension if applicable) to be deleted. Here is an example:
if exist Desktop (rmdir /s/q Desktop)

cmd if

How to remove files and folders using Windows PowerShell

The commands in Windows PowerShell to delete and remove content from your PC are very much similar to those of Command Prompt, with a few additional aliases. The overall functionality and logic are the same.

We recommend that you launch Windows PowerShell with administrative privileges before proceeding.

The main thing to note here is that unlike Command Prompt, all commands can be used for both purposes – removing individual files as well as complete directories. We ask you to be careful while using PowerShell to delete files and folders, as the directory itself is also removed.

The good thing is that you do not need to specify recursive action. If a directory has sub-directories, PowerShell will confirm whether you wish to continue with your deletion, which will also include all child objects (subdirectories).

Here is a list of all the commands/aliases that can be used in PowerShell to remove an item:

  • Del
  • Rm-dir
  • remove-item
  • Erase
  • Rd
  • Ri
  • Rm

We tested all of these commands in our working directory and each of them was successful in deleting the folders as well as individual items, as can be seen below:

PS all

As can be seen above, the syntax of all the aliases is the same. You can use any of the commands below to delete an item using PowerShell:

Del File/FolderName
Rm-dir File/FolderName
remove-item File/FolderName
Erase File/FolderName
Rd File/FolderName
Ri File/FolderName
Rm File/FolderName

Delete multiple files and folders

You can also delete multiple selective files and folders just as we did while using Command Prompt. The only difference is that you will need to provide the complete path of each item, even if you are in the same working directory. Use the command below to do so:

Del "DriveLetter:PathItemName", "DriveLetter:PathItemName"

Remember to append the file type if the item is not a directory (.txt, .png, etc.), as we have done in the example below:

PS delete selective 1

You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command, as done in Command Prompt. Here is an example:

PS asterisk

The command shown above will remove all.txt files in the directory “New folder.”

Delete files and folders in any directory

You can also remove an item in a different directory, just like we did in Command Prompt. Simply enter the complete path to the item in PowerShell, as we have done below:

PS different location

Delete files and folders with complex and long paths using the command line

Sometimes you may encounter an error while trying to delete an item that may suggest that the path is too long, or the item cannot be deleted as it is buried too deep. Here is a neat trick you can apply using both Command Prompt and PowerShell to initially empty the folder, and then remove it using any of the methods above.

Use the command below to copy the contents of one folder (which is empty) into a folder that cannot be deleted. This will also make the destination folder empty, hence making it removable.

robocopy "D:EmptyFolder" D:FolderToRemove /MIR

In this scenario, the EmptyFolder is the source folder that we have deliberately kept empty to copy it to the target folder “FolderToRemove.”

robocopy

You will now see that the folder that was previously unremovable is now empty. You can proceed to delete it using any of the methods discussed in this article.

Closing words

The command line is a blessing for Windows users. You can use any of these commands to remove even the most stubborn files and folders on your computer.

Let us know which solution worked for you in the comments section down below.

Also see:

Subhan Zafar is an established IT professional with interests in Windows and Server infrastructure testing and research, and is currently working with Itechtics as a research consultant. He has studied Electrical Engineering and is also certified by Huawei (HCNA & HCNP Routing and Switching).

Updated: 02/07/2022 by

Delete file image

Now and then, it is a good idea to clean up your drives and delete duplicate photos, documents, temporary files, shortcuts, videos, or other unneeded or unused files or folders. The steps to delete a computer file, directory, or folder vary on the method you want to use and your operating system. To proceed, choose from the list of options below and follow the instructions.

How to delete files in Microsoft Windows

Microsoft Windows users can delete an unwanted file or folder (directory) from a hard drive or external disk drive using many different methods. Below are the more common methods for deleting a file or folder in Microsoft Windows.

Note

Users not familiar with Windows should realize that if you delete a folder or directory, all files and folders in that folder or directory are deleted.

Delete key

Locate the item you want to delete, highlight it by left-clicking the file or folder with your mouse once, and press Delete. You can browse the location of the file or folder using My Computer or Windows Explorer.

Tip

You can delete multiple files or folders by holding down Ctrl and clicking each file or folder before pressing Delete.

Tip

You can hold down Shift while pressing Delete to prevent files from going to the Recycle Bin when deleted.

Right-click delete a file in Windows

Delete file or folder by right-clicking

Open My Computer or Windows Explorer. We recommend you make sure the directory or folder is empty before proceeding, unless you intend to delete everything in it. Locate the file or folder you want to delete and right-click it. Choose the Delete option from the pop-up menu.

How to delete from the local disk

Important

The local disk contains files and folders that are imperative for your computer to run correctly. Unless you know what you are deleting, please do not delete any files from this section.

Open My Computer or Windows Explorer. On the left side of the screen, click This PC. On the right side of the screen, locate and double-click the local disk (usually C: or D:). Double-click the folder containing the file you want to delete. Select the file or folder you want to delete, click File in the top menu bar, and select Delete.

How to delete from an external drive

To delete from a USB flash drive, floppy drive, memory card, or external hard drive, open My Computer or Windows Explorer. On the left side of the screen, click This PC. On the right side of the screen, locate and double-click the drive, which is labeled as USB, flash drive, external hard drive, or the manufacturer’s name. Select the file or folder you want to delete, click File in the top menu bar, and select Delete.

Delete from the file menu

Open My Computer or Windows Explorer. Locate and select the file or folder you want to delete, click File in the top menu bar, and select Delete.

Tip

If the File menu is not visible in My Computer or Windows Explorer, press Alt to make the menu bar visible, including the file menu.

Problems during delete

Some documents and folders may be protected from deletion through encryption or password protection. In this case, you may be asked for a password to decrypt or remove the password protection.

A file may be set as read-only, meaning the user can only open it for viewing and not modify or delete it. When trying to delete a read-only file, you get a message stating the file is write-protected and cannot be deleted. You need to modify or write permissions to delete the file.

Some files may only be deleted with administrator permissions. To delete these files, you would need to have administrator rights on the computer. If you are using a work computer, the technical support staff often are the only users with administrator rights on the computer.

Another possible cause of problems with deleting a file or folder is a virus or malware infection. Viruses and malware can prevent files or folders from being modified or deleted. If this is the case, you need to remove the virus or malware infection to delete the affected file or folder.

  • Unable to delete file: being used by another person or program.
  • How to remove a virus and malware from my computer.

Windows command line

See the MS-DOS and Windows command line section below for information about deleting a file or folder at the Windows command line.

Uninstalling a program

See our uninstalling a program steps for help with uninstalling (deleting) software programs from the computer.

  • How to uninstall software in Windows.

How to restore a deleted file or folder

If you’ve deleted a file by mistake, you can see our page on how to restore a deleted file for further information on recovering a deleted file.

  • How to recover missing, lost, or deleted files.

How to delete files in MS-DOS and the Windows command line

Note

Keep in mind that any deleted file or directory in MS-DOS is not sent to the Windows Recycle Bin.

Before you follow any of the steps below, you must get to an MS-DOS prompt or the Windows command line. If you are new to the command line, you may also want to read through the following pages first.

  • How to get to an MS-DOS prompt or Windows command line.
  • How to use the Windows command line (DOS).

del command

Files

MS-DOS users can delete files using the del command. See this page to get additional information and help with this command. Below is an example of how to use this command.

del example.txt

As seen in the example above, when deleting a file, you need to enter the full file name, including the file extension.

Tip

The del command can delete any file.

Delete multiple files

You can also use wildcards to delete multiple files or a group of files, as shown in the example below.

del *.txt

In the example above, this command would delete all text files that end with a .txt file extension.

Tip

The del command can delete any file extension.

Directory

MS-DOS users can delete directories (dir) in MS-DOS using the deltree command or rmdir command. See either of these links for additional information about these commands. Below is an example of how to use this.

rmdir example

Note

If the directory is full or has other subdirectories, you get an error message. To delete a full directory, you need to use a switch with the above example. For example, «rmdir example /s» to remove a full «example» directory. See our deltree command or rmdir command for additional examples and switches.

  • How to delete files in MS-DOS without a prompt.

Deleting a subdirectory

To delete a subdirectory, subfolder, folder within a folder, or directory within a directory, use a command similar to the example below.

rmdir exampletest

In the example above, the «test» directory in the «example» directory is deleted. You could also use the cd command to change the directory to the example directory and then delete the «test» directory using our first example shown above.

How to delete a directory or file name with a space

To delete a directory or file name with a space in the name, you must surround the directory or file name with quotes, as shown below.

del "my example file.txt"
rmdir "my example directory"

In the above examples, we are deleting the file named «my example file.txt» with quotes surrounding the complete file name and extension and removing the «my example directory» directory.

Tip

The rmdir command can delete any file.

  • How to use the Windows command line (DOS).

How to delete files in Linux, Unix, and other variants

rm command

Files

Linux and Unix users can delete files through the console using the rm command. See this page for additional information about this command. Below is an example of how to use this command.

rm example.txt

As seen in the example above, when deleting a file, you need to enter the full file name, including the file extension.

Tip

The rm command can delete any file.

Delete multiple files

You can also use wildcards if you want to delete multiple files, as shown in the example below.

rm *.txt

In the example above, this command would delete all files with a .txt file extension.

Tip

The rm command can delete any file of file extensions.

Directory

Linux and Unix users can delete folders through the console with the rmdir command. See this page for additional information about this command. Below is an example of how to use this command.

rmdir example

Tip

Like Microsoft Windows, with Linux and Unix, you can also delete files through the GUI by locating the file and pressing the delete key on the keyboard.

Deleting a subdirectory

To delete a directory in another directory (subdirectory), use a command similar to the example below.

rmdir exampletest

In the example above, the «test» directory in the «example» directory would be deleted. You could also use the cd command to change the directory to the example directory and then delete the «test» directory using our first example shown above.

How to delete a directory or file name with a space

To delete a directory or file name with a space in the name, you must surround the directory or file name with quotes, as shown below.

rm "my example file.txt"
rmdir "my example directory"

In the examples above, we are deleting the file named «my example file.txt» with quotes surrounding the complete file name and extension. We are also removing the «my example directory» directory.

Tip

The rmdir command can delete any file.

  • How to remove a full directory in Linux.
  • Linux and Unix shell tutorial.

How to delete files on macOS

Apple macOS users can delete photos, documents, or other files or folders (directory) on their Mac using many different methods. Below are the more common methods for deleting a file or folder.

Note

Users not familiar with Apple macOS should realize that it deletes all the files in a folder if you delete that folder.

Delete key

The delete key on the keyboard by itself does not delete a file or folder on macOS. To delete a file or folder, press and hold Command, then press delete. You can browse to the location of the file or folder using Finder.

Right-click and choosing Move to Trash

Open Finder, locate the file or folder you want to delete, and right-click the file or folder. In the right-click menu that appears, click the Move to Trash option.

Delete from the file menu

Open Finder, locate and select the file or folder you want to delete. Click File in the top menu bar and select Move to Trash.

Terminal

To delete files or directories in the Terminal command line, use the rm command.

How to delete files on Microsoft Windows 3.X

File Manager

  1. Open File Manager
  2. Locate the folder or file you want to delete, then click File and Delete.

MS-DOS

See the MS-DOS user section above for information about deleting a directory in MS-DOS.

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    In some cases, for whatever reason, Windows will make sure that the provided file is used by the system and prevent it from being deleted. This file situation is very frustrating, especially if you know the file is not in use.

    If you are having trouble in deleting any file or folder directly by right-clicking, then you can delete it using cmd. The commands below delete the specific file or folder and place them in the recycle bin:

    • del
    • rmdir

    Here we have created a sample File and a Folder to delete it using CMD:

    del command

    del command is used to delete a file. Here, we will take our sample file “hello.txt” located at the desktop and try to delete it using the del command in CMD. Follow the steps given below to delete the file:

    Step 1: Change the path of the directory in CMD and set it to the path of the file. Type the following command in cmd and press Enter:

    cd desktop

    Step 2: Delete the file “hello.txt” with following command:

     del hello.txt

    rmdir command

    rmdir command is used to delete the entire folder or directory. Here, we will take our sample folder named “Tasks” placed at desktop and try to delete it using rmdir command in CMD. Follow the steps given below to delete the folder:

    Step 1: Change the path of the directory in CMD and set it to the path of the folder. Type the following command in cmd and press Enter:

    cd desktop

    Step 2: Delete the folder “Tasks” with following command:

    rmdir tasks

    Windows provides the command-line interface (cmd) for various operations where one of them is deleting files. Windows provides del, rm commands in order to delete specified file or files from the command line interface with various options. In the tutorial, we will learn how to delete a file or files from command-line interface (cmd) with different commands in various ways.

    Delete File with del Command

    The del command is very old command provided with the first versions of the MS-DOS. the del command can be used to delete file or files via cmd in different ways.

    Delete Single File

    We will start with a simple example. The del command can be used to delete or remove a single file via the command line interface.

    del myfile.txt

    If the file name contains space or special characters using the double quoto is a safe way to prevent errors based file name.

    del "my file.txt"

    The most reliable way to delete a single file is provide its absolute path or full path inside the double quotos like below.

    del "C:datamy file.txt"

    Delete Multiple Files

    The del command can be used to delete multiple files. Just provide the file names by separating them spaces. We can see that by using double quotes multiple files with file names containing space can be easily deleted.

    del "my file.txt" "yourfile.txt" "1file.txt"

    Delete Specific File Extension

    File extension are used to specify and display file content and file type. also the del command can be used to delete or remove specific file types according to their file extensions. For example “*.txt” is used specify text files.

    del "*.txt"

    We can also specify the absolute path or full path where all files with the specifed extension will be removed.

    del "D:data*.txt"

    Delete Specified File Names

    Similar to the file extension different file name patterns can be specified for the delete or remove operation. The glob “*” can be used to express different file names. In the following example, we will delete all files whose names start with “A”.

    del "A*"

    Delete Read Only File

    Read-only files are cannot be deleted by default. But the del command provides the /F or /a:R option in order to delete read-only files.

    del /F "*"

    Понравилась статья? Поделить с друзьями:
  • Config ini где находится windows 10
  • Command conquer 4 tiberian twilight windows 10
  • Conexant smartaudio hd windows 7 нет звука
  • Command conquer 3 tiberium wars не запускается на windows 10
  • Conexant smartaudio hd driver windows 7 64 bit скачать