Delete files or folder recursively on Windows CMD
Asked Answered
L

15

195

How do I delete files or folders recursively on Windows from the command line?

I have found this solution where path we drive on the command line and run this command.

I have given an example with a .svn file extension folder:

for /r %R in (.svn) do if exist %R (rd /s /q "%R")
Liva answered 5/10, 2012 at 14:51 Comment(1)
Does this answer your question? "rm -rf" equivalent for Windows?Clerk
S
230

The other answers didn't work for me, but this did:

del /s /q *.svn
rmdir /s /q *.svn

/q disables Yes/No prompting

/s means delete the file(s) from all subdirectories.

Spleenwort answered 11/7, 2015 at 23:53 Comment(1)
I think my needs are the same as what you are trying to do - I'm trying to delete all the .svn folders in a project directory and all subdirectories. On Windows 2012, this answer isn't working for me. It is deleting the .svn folder in the current directory, but not from any of the child directories.Arbe
E
146

Please execute the following steps:

  1. Open the command prompt
  2. Change directory to the required path
  3. Give the following command

    del /S *.svn
    
Eastsoutheast answered 9/10, 2014 at 14:9 Comment(1)
Does not work for me, it deletes only the files in the current directory.Sopher
A
69

You can use this in the bat script:

rd /s /q "c:\folder a"

Now, just change c:\folder a to your folder's location. Quotation is only needed when your folder name contains spaces.

Antiphonal answered 5/10, 2012 at 15:37 Comment(3)
This command is not working and throwing error as below D:\>rd /s /q "D:\Root a" The system cannot find the file specified.Phinney
Unless your directory really does have a space a in the name, then leave out that part.Edroi
TIL: rd is a valid command. Thanks, this solution worked!Lipinski
N
34
RMDIR path_to_folder /S

ex. RMDIR "C:\tmp" /S

Note that you'll be prompted if you're really going to delete the "C:\tmp" folder. Combining it with /Q switch will remove the folder silently (ex. RMDIR "C:\tmp" /S /Q)

Neuberger answered 22/7, 2014 at 5:19 Comment(0)
C
10

For file deletion, I wrote following simple batch file which deleted all .pdf's recursively:

del /s /q "\\ad1pfrtg001\AppDev\ResultLogs\*.pdf"
del /s /q "\\ad1pfrtg001\Project\AppData\*.pdf"

Even for the local directory we can use it as:

del /s /q "C:\Project\*.pdf"

The same can be applied for directory deletion where we just need to change del with rmdir.

Cassation answered 23/7, 2015 at 19:44 Comment(0)
G
6

If you want to delete a specific extension recursively, use this:

For /R "C:\Users\Desktop\saleh" %G IN (*.ppt) do del "%G"
Giltzow answered 28/8, 2016 at 12:23 Comment(0)
D
6

Use the Windows rmdir command

That is, rmdir /S /Q C:\Temp

I'm also using the ones below for some years now, flawlessly.

Check out other options with: forfiles /?

Delete SQM/Telemetry in windows folder recursively

forfiles /p %SYSTEMROOT%\system32\LogFiles /s /m *.* /d -1 /c "cmd /c del @file"

Delete windows TMP files recursively

forfiles /p %SYSTEMROOT%\Temp /s /m *.* /d -1 /c "cmd /c del @file"

Delete user TEMP files and folders recursively

forfiles /p %TMP% /s /m *.* /d -1 /c "cmd /c del @file"
Donovan answered 3/8, 2018 at 16:12 Comment(0)
T
6

For completely wiping a folder with native commands and getting a log on what's been done.

here's an unusual way to do it :

let's assume we want to clear the d:\temp dir

mkdir d:\empty
robocopy /mir d:\empty d:\temp
rmdir d:\empty
Threesome answered 28/2, 2020 at 18:29 Comment(3)
This is a very clever solution that solved my issue. I used robocopy to copy my AppData directory, not thinking about the self referencing Junction. This was the only way I could remove all of the hidden files recursively. Never forget the /xj!Peddler
Brilliant! This works really, really well. I found myself in a "loop" of trying to delete files owned by TrustedInstaller. Explorer would prompt to get permission but it the operation would ultimately fail. Scenario was trying to delete a Windows folder of an offline Windows install. This method worked perfectly!Precession
I found the /xj essential when deleting bin and obj in Visual Studio. Thanks guys.Aw
R
4

You could also do:

del /s /p *.{your extension here}

The /p will prompt you for each found file, if you're nervous about deleting something you shouldn't.

Ruthenian answered 5/1, 2017 at 18:7 Comment(0)
C
4

The Windows Command Processor cmd.exe has two internal commands for deletion of files and folders:

  1. The command DEL is for the deletion of files with usage help output on running in a Windows command prompt window either help del or del /?.
  2. The command RMDIR or with shorter name RD is for removal of directories with usage help output on running in a Windows command prompt window either help rmdir or rmdir /? or help rd or rd /?.

Deletion of all *.svn files in an entire folder tree

There can be used in a Windows command prompt window or a Windows batch file the following command to delete really all files of which long or short 8.3 file name is matched by the wildcard pattern *.svn in the directory %USERPROFILE%\Projects or any of its subdirectories:

del /A /F /Q /S "%USERPROFILE%\Projects\*.svn" >nul 2>&1

The usage of option /A to match all files independent on the file attributes replaces the implicit default /A-H to ignore hidden files. So even files with hidden attribute are deleted by this command because of using the option /A. Files matched by wildcard pattern *.svn with hidden attribute set are ignored on not using the option /A.

The option /F forces a deletion of files with file extension .svn which have the read-only attribute set. There would be output the error message Access is denied. if a *.svn file has the read-only attribute set and the option /F is not used on running the command DEL.

The quiet option /Q prevents the user confirmation prompt Are you sure (Y/N)?.

The option /S results in searching not only in the specified directory, but also in all its subdirectories including those with hidden attribute set even on not using option /A for files of which long or short 8.3 name is matched by the wildcard pattern *.svn.

The two redirections >nul and 2>&1 result in redirecting the list of deleted files output to handle STDOUT (standard output) and the error messages output to handle STDERR (standard error) to the device NUL to suppress every output.

There are deleted also hard links and symbolic links matched by the wildcard pattern *.svn on using this command, but not the files linked to on having a file name not ending with .svn or being in a different directory tree.

Files matched by the wildcard pattern *.svn currently opened by a process (program/application) with using shared access permissions to deny all other processes to delete the file as long as being opened by this process are not deleted by this command. File system permissions can result also in files not being deleted by this command.

Deletion of all *.svn folders in an entire folder tree

There can be used in a Windows command prompt window the following command to remove really all folders matching in long or short 8.3 folder name the wildcard pattern *.svn in the directory %USERPROFILE%\Projects and all its subdirectories:

for /F "delims=" %I in ('dir "%USERPROFILE%\Projects\*.svn" /AD /B /S 2^>nul') do @rd /Q /S "%I" 2>nul

The same command line for usage in a batch file containing @echo off at top is:

for /F "delims=" %%I in ('dir "%USERPROFILE%\Projects\*.svn" /AD /B /S 2^>nul') do rd /Q /S "%%I" 2>nul

There is executed on more cmd.exe in background with option /c and the command line specified between ' as additional arguments to run in background the Windows Command Processor internal command DIR to search

  • in the specified directory %USERPROFILE%\Projects
  • and in all its subdirectories because of option /S
  • for just directories because of using the option /AD which includes also junctions and symbolic directory links
  • matching the wildcard pattern *.svn.

The file system entries (= directory names) matched by these criteria are output in bare format because of option /B with full path because of option /S to handle STDOUT of the background command process without surrounding " even on full directory name containing a space or one of these characters &()[]{}^=;!'+,`~. The error message output by DIR on not finding any name matching these criteria is redirected to device NUL to suppress it.

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

The output list of directory names with their full paths to handle STDOUT is captured by cmd.exe processing the batch file and processed by FOR after started cmd.exe closed itself.

The FOR /F option delims= defines an empty list of string delimiters which results in each entire directory name is assigned completely one after the other to the specified loop variable I.

The command RD is executed to delete quietly because of option /Q the directory with all files and all subdirectories because of option /S.

There are deleted also junctions (soft links) and symbolic directory links matched by the wildcard pattern *.svn on using this command, but not the directories linked to on having a directory name not ending with .svn or being in a different directory tree.

A directory matched by the wildcard pattern *.svn in which a file is currently opened by a process (program/application) with using shared access permissions to deny all other processes to delete the file as long as being opened by this process is not deleted by this command and of course also no directory above the directory containing the file which cannot be deleted at the moment. File system permissions can result also in directories not being deleted by this command. Windows prevents by default also the deletion of a directory which is the current working directory of any running process.

Other useful information regarding to deletion of files and folders

The directory path %USERPROFILE%\Projects\ can be removed completely or replaced by .\ in the commands above to delete the files and folders matching the wildcard pattern *.svn in the current directory of the Windows Command Processor process which executes the commands.

The directory path %USERPROFILE%\Projects\ can be replaced by %~dp0 to delete the files and folders matching the wildcard pattern *.svn in the directory of the batch file on using the command lines above in a batch file independent on which directory is the current directory on execution of the batch file.

The directory path %USERPROFILE%\Projects\ can be replaced also by a relative path. Please read the Microsoft documentation about Naming Files, Paths, and Namespaces for more details about relative paths.

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

  • del /?
  • dir /?
  • for /?
  • rd /?

Run mklink /? for help on how to create file and directory links explained very well by MKLink.

See also:

Chukker answered 20/3, 2022 at 14:20 Comment(0)
G
3
dir -Recurse *.[extension] |del
Gulp answered 8/11, 2022 at 10:55 Comment(1)
Welcome to Stack Overflow! Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.Frasch
C
2

For hidden files I had to use the following:

DEL /S /Q /A:H Thumbs.db
Carlist answered 20/8, 2018 at 20:16 Comment(1)
I had to use this to delete .dcm files that were left over from a backup restore (windows 10)Elective
F
1

It worked for me

del /s /q "dir_name"
Farmann answered 15/7, 2021 at 6:54 Comment(0)
J
0
dir /b %temp% >temp.list
for /f "delims=" %%a in (temp.list) do call rundll32.exe advpack.dll,DelNodeRunDLL32 "%temp%\%%a"
Jaeger answered 30/3, 2020 at 16:16 Comment(1)
Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.Frasch
W
0

You can use PowerShell to achieve this. Here's the command to delete all JSON files in the main folder and its subfolders:

Get-ChildItem -Path "C:\path\to\main\folder" -Filter *.json -File -Recurse | Remove-Item -Force

Replace C:\path\to\main\folder with the actual path to your main folder. This command will recursively find all JSON files in the specified folder and its subfolders and delete them.

Winglet answered 24/2, 2024 at 15:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.