I use Windows.
I want to delete all files and folders in a folder by system call.
I may call like that:
>rd /s /q c:destination
>md c:destination
Do you know an easier way?
rene
40.8k78 gold badges118 silver badges149 bronze badges
asked Oct 1, 2009 at 9:32
2
No, I don’t know one.
If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:
del /q destination*
for /d %x in (destination*) do @rd /s /q "%x"
This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).
Note that within a batch file you need to double the %
within the for
loop:
del /q destination*
for /d %%x in (destination*) do @rd /s /q "%%x"
answered Oct 1, 2009 at 9:41
JoeyJoey
339k83 gold badges680 silver badges679 bronze badges
12
del c:destination*.* /s /q
worked for me. I hope that works for you as well.
ra.
1,79814 silver badges15 bronze badges
answered Oct 11, 2012 at 19:45
SeanSean
4634 silver badges2 bronze badges
5
I think the easiest way to do it is:
rmdir /s /q "C:FolderToDelete"
The last «» in the path is the important part.
This deletes the folder itself. To retain, add mkdir "C:FolderToDelete"
to your script.
AlainD
4,8563 gold badges37 silver badges90 bronze badges
answered Nov 20, 2014 at 10:26
BananBanan
4334 silver badges2 bronze badges
6
Yes! Use Powershell:
powershell -Command "Remove-Item 'c:destination*' -Recurse -Force"
answered Feb 16, 2017 at 15:00
Rosberg LinharesRosberg Linhares
3,3891 gold badge31 silver badges33 bronze badges
1
If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.
set targetdir=c:example
del /q %targetdir%*
for /d %%x in (%targetdir%*) do @rd /s /q ^"%%x^"
answered Jan 24, 2014 at 10:05
fractorfractor
1,4942 gold badges14 silver badges30 bronze badges
To delete file:
del PATH_TO_FILE
To delete folder with all files in it:
rmdir /s /q PATH_TO_FOLDER
To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.*
cannot delete folders, but removes files from all subfolder. So two commands are needed:
del /q PATH_TO_FOLDER*.*
for /d %i in (PATH_TO_FOLDER*.*) do @rmdir /s /q "%i"
You can create a script to delete whatever you want (folder or file) like this mydel.bat
:
@echo off
setlocal enableextensions
if "%~1"=="" (
echo Usage: %0 path
exit /b 1
)
:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1
:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%
Few example of usage:
mydel.bat "pathtofolder with spaces"
mydel.bat pathtofile_or_folder
answered Nov 10, 2016 at 17:44
Maxim SuslovMaxim Suslov
4,0111 gold badge30 silver badges29 bronze badges
One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY
(http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR
switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.
Also probably want to set a value for the retry switch, /r
, because the default number of retries is 1 million.
robocopy "C:DoNotDelete_UsedByScriptsEmptyFolder" "c:tempMyDirectoryToEmpty" /MIR /r:3
answered May 27, 2014 at 15:43
BateTechBateTech
5,6423 gold badges19 silver badges31 bronze badges
I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:
for /d in (*) do rd /s /q "%a" & (
md "%a")
I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.
SteveTurczyn
35.5k6 gold badges40 silver badges53 bronze badges
answered Jul 8, 2014 at 19:43
0
answered Jun 20, 2016 at 17:41
NoWarNoWar
35.7k78 gold badges317 silver badges490 bronze badges
1
It takes 2 simple steps. [/q means quiet, /f means forced, /s means subdir]
-
Empty out the directory to remove
del *.* /f/s/q
-
Remove the directory
cd .. rmdir dir_name /q/s
answered May 11, 2020 at 21:51
Jenna LeafJenna Leaf
2,20321 silver badges29 bronze badges
1
try this, this will search all MyFolder under root dir and delete all folders named MyFolder
for /d /r "C:Userstest" %%a in (MyFolder) do if exist "%%a" rmdir /s /q "%%a"
Paul Roub
36.2k27 gold badges82 silver badges89 bronze badges
answered Jul 14, 2020 at 17:57
del .*
This Command delete all files & folders from current navigation in your command line.
answered Nov 14, 2020 at 9:11
I want to remove all files from a folder structure, so I’m left with an empty folder structure.
Can this be achieved in either batch or VBScript scripting?
I have tried a very basic batch command, but this required the user to allow the deletion of each file. This wasn’t a suitable solution as there are many hundreds of files and this will increase massively over time.
What can you suggest?
asked Apr 15, 2014 at 12:40
6
This can be accomplished using PowerShell:
Get-ChildItem -Path C:Temp -Include *.* -File -Recurse | foreach { $_.Delete()}
This command gets each child item in $path
, executes the delete method on each one, and is quite fast. The folder structure is left intact.
If you may have files without an extension, use
Get-ChildItem -Path C:Temp -Include * -File -Recurse | foreach { $_.Delete()}
instead.
It appears the -File
parameter may have been added after PowerShell v2. If that’s the case, then
Get-ChildItem -Path C:Temp -Include *.* -Recurse | foreach { $_.Delete()}
It should do the trick for files that have an extension.
If it does not work, check if you have an up-to-date version of Powershell
answered Apr 15, 2014 at 13:18
MDMoore313MDMoore313
5,8461 gold badge26 silver badges31 bronze badges
13
Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.
Remove-Item c:Tmp* -Recurse -Force
answered Nov 2, 2017 at 17:21
Evan NadeauEvan Nadeau
1,2031 gold badge7 silver badges2 bronze badges
7
You can do so with del
command:
dir C:folder
del /S *
The /S
switch is to delete only files recursively.
answered Apr 15, 2014 at 12:44
ek9ek9
3,3253 gold badges19 silver badges20 bronze badges
8
Using PowerShell:
Get-ChildItem -Path c:temp -Include * | remove-Item -recurse
bwDraco
45.3k43 gold badges163 silver badges202 bronze badges
answered Oct 28, 2014 at 15:39
Reading between the lines on your original question I can offer an alternative BATCH code line you can use. What this will do when ran is only delete files that are over 60 days old. This way you can put this in a scheduled task and when it runs it deletes the excess files that you don’t need rather than blowing away the whole directory. You can change 60 to 5 days or even 1 day if you wanted to. This does not delete folders.
forfiles -p "c:pathtofiles" -d -60 -c "cmd /c del /f /q @path"
answered Apr 15, 2014 at 13:18
TravisTravis
1,0548 silver badges16 bronze badges
2
Use PowerShell to Delete a Single File or Folder. Before executing the Delete command in powershell we need to make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.
With Example: http://dotnet-helpers.com/powershell-demo/how-to-delete-a-folder-or-file-using-powershell/
Using PowerShell commnads to delete a file
Remove-Item -Path «C:dotnet-helpersDummyfiletoDelete.txt»
The above command will execute and delete the “DummyfiletoDelete.txt” file which present inside the “C:dotnet-helpers” location.
Using PowerShell commnads to delete all files
Remove-Item -Path «C:dotnet-helpers*.*»
Using PowerShell commnads to delete all files and folders
Remove-Item -Path «C:dotnet-helpers*.*» -recurse
-recurse
drills down and finds lots more files. The –recurse
parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.
Using -Force command to delete files forcefully
Using PowerShell command to delete all files forcefully
Remove-Item -Path «C:dotnet-helpers*.*» -Force
answered May 10, 2018 at 7:13
Try this using PowerShell. In this example I want to delete all the .class files:
Get-ChildItem '.FOLDERNAME' -include *.class -recurse | foreach ($_) {remove-item $_.FullName}
answered Nov 20, 2014 at 21:28
BacaraBacara
1112 bronze badges
-
In Windows Explorer select the root dir containing all the files and folders.
-
Search for *
-
Sort by Type (All the folders will be at the top and all the files listed underneath)
-
Select all the files and press Delete.
This will delete all the files and preserve the directory structure.
answered Mar 16, 2015 at 9:12
Delete all files from current directory and sub-directories but leaving the folders structure.
(/Q
) switch is for asking the user if he is ok to delete
Caution : try it without the /Q
to make sure you are not deleting anything precious.
del /S * /Q
answered May 18, 2015 at 11:30
This is the easiest way IMO
Open PowerShell, navigate to the directory (cd
), THEN
ls -Recurse * | rm
(Poof) everything is gone…
If you want to delete based on a specific extension
ls -Recurse *.docx | rm
ls
is listing the directory
-Recurse
is a flag telling powershell to go into any sub directories
*
says everything
*.doc
everything with .doc extension
|
feed the output from the left to the right
rm
delete
All the other answers appear to make this more confusing than necessary.
answered Oct 12, 2016 at 20:45
Kellen StuartKellen Stuart
5282 gold badges8 silver badges17 bronze badges
We can delete all the files in the folder and its sub folders via the below command.
Get-ChildItem -Recurse -Path 'D:Powershell Practice' |Where-Object{$_.GetType() -eq [System.IO.FileInfo]} |Remove-Item
Or
Get-ChildItem -Recurse -Path 'D:Powershell Practice' -File | Remove-Item
answered Mar 1, 2020 at 6:34
1
dir C:testx -Recurse -File | rd -WhatIf
What if: Performing the operation "Remove File" on target "C:testxx.txt".
What if: Performing the operation "Remove File" on target "C:testxblax.txt".
bertieb
7,23436 gold badges40 silver badges52 bronze badges
answered Apr 10, 2017 at 10:36
With Powershell 5.1:
$extensions_list = Get-ChildItem -Path 'C:folder_path' -Recurse
foreach ( $extension in $extensions_list) {
if ($extension.Attributes -notlike "Directory") {
Remove-Item $extension.FullName
}
}
It’s removes all itens that are not Directory.
$extension.FullName = Item Path
$extension.Attributes = Item Type ( Directory or Archive )
answered Feb 4, 2020 at 17:43
PowerShell example.
I found some paths don’t play nicely, so using -LiteralPath
works in all cases.
Help on which can be found on the MS docs for Remove-Item
.
# To delete all files within a folder and its subfolders.
$subDir = "Z:a path tosomewhere"
# Remove the -WhatIf and -Verbose here once you're happy with the result.
Get-ChildItem -LiteralPath $subDir -File -Recurse | Remove-Item -Verbose -WhatIf
answered Jun 11, 2021 at 22:54
SteSte
1,0711 gold badge9 silver badges22 bronze badges
If all your files have an extension and none of your folders have a dot, you can do the following:
Remove-Item C:Test* -Include *.* -Recurse
Check the official documentation which provides multiples examples.
answered Feb 17, 2022 at 10:53
NereisNereis
1012 bronze badges
As a complement to the above answers, actually there’s no need to use the Get-Childitem and pass the result to the pipeline in the above answers, because the -Include keyword is included in the Remove-Item command
One can simply:
Remove-Item -Include «.» «C:Temp» -Recurse
answered Oct 14, 2018 at 14:59
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»:
After that, you’ll see a Command Prompt window with administrative privileges:
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:
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:
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:
To get around this, use the /f
flag to force delete the file. For example, del /f "Read Only Test File.txt"
:
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
:
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:
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
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
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:
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.
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:
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:
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:
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.
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"
The above command will remove the “New folder” only if it is empty. If a folder has subdirectories, you might get the following prompt:
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"
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"
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"
For directories:
rd "Folder1" "Folder3" "Folder5"
Here is a before and after comparison of the directory where both of the above commands were executed:
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"
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:
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)
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:
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:
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:
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:
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.”
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).
In some cases, Windows is not allowing some users to delete their files or folders. You can use the command prompt to delete a file or a folder if you encounter such a problem. In this article, we will show you how exactly you can use this method to delete files and folders.
- How to delete files on Windows 10 with CMD
- How to delete folders on Windows 10 with CMD
- Force Uninstall
Deleting files on Windows 10 with command prompt
The built-in del command can help you delete files on Windows 10. You have to point out the specific path to this file on your PC.
To use this method do the following:
- Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
- Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
del path
- After that press Enter
Lets’ see this example to clarify the whole process:
You want to delete a file called Info.txt that is located on your desktop. Use the following command in the Command Prompt where you replace username with your own username:
Del “C:UsersusernameDesktopInfo.txt”
After you enter this command the file will be deleted from the Desktop.
There are several commands that you can use to modify a bit the del command.
For instance, you can add the /p parameter to the command to get Command Prompt to be asked for confirmation before deleting a file.
You can also add the /f parameter to force delete a read-only file.
You can also use Revo Uninstallers’ force uninstall feature if you have trouble removing stubborn programs.
To delete folders in Windows 10 with CMD you have to use the rmdir command
This command will remove all folders including the subfolders and files in them.
To use this command do the following:
- Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
- Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
rmdir PATH
- Press Enter to finish the process
For example, to delete a folder named “Info” on your desktop, use the following command where you will replace username with your own username:
rmdir “C:UsersusernameDesktopInfo”
After you press Enter the folder named “Info” on your desktop will be deleted.
If you want to delete all the files and subfolders in the folder you want to delete, add the /s parameter. This will ensure that the folder is removed including the subfolders and files in it.
Conclusion
As you’ve noticed, commands to delete files and folders in Windows 10 can be pretty handy if you have trouble deleting them the regular way.
Post Views: 9,291
Updated: 02/07/2022 by
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.
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).
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
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
- Open File Manager
- 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.
How to delete files recursively with the cmd command prompt in Windows?
How to delete files with cmd, from a folder and all its subfolders. Use the del batch command line and the recursive option. The Windows delete command provides this useful option automate the deletion process. For example, if you have a huge amount to delete but need to keep the folder and subfolder’s structure.
Indeed, it’s the main goal of this tutorial, to be effective and avoid browsing all the subfolders in order to search for a specific file name or extension. The main option that I suggest using is the deletion confirmation, this way you can control one last time which files are recursively deleted. Please note that the files deleted in recursive mode with the command line are not stored in the Windows Recycle Bin.
Make sure you delete the proper files only. The operation is irreversible.
Delete files from folders and subfolders using del
To delete files recursively using the explicit path, without any confirmation prompt, use this command:
del /s "C:Folder"
You can also use this variation, without the quotes and without any confirmation prompt. The quotes are mandatory when the complete path of the folder, or the file, contains spaces.
del /s C:Folder
Suppress all files with a confirmation
This option is the one I recommend. Indeed, make sure to double check and confirm before all files are deleted.
Please note that this option deletes only the text files as we use the *.txt filter.
del /p /s SubFolder_1*.txt
Then a prompt is displayed for the files that match, in alphabetical order:
c:FolderSubFolder_1file (1).txt, Delete (Y/N)?
Delete files and folders without confirmation
To force the deletion with no prompt at all use the delete quiet mode. Useful with a lot of files.
del /q /s SubFolder_2*.txt
To conclude, the del /s recursive option is the fastest and easiest way to delete files recursively with cmd in Windows. Here is how to copy recursively files and folders using the xcopy command.