How to delete all files and folders in a folder by cmd call

WindowsBatch File

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

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"

Solution 2 - Windows

del c:\destination\*.* /s /q worked for me. I hope that works for you as well.

Solution 3 - Windows

I think the easiest way to do it is:

rmdir /s /q "C:\FolderToNotToDelete\"

The last "" in the path is the important part.

Solution 4 - Windows

Yes! Use Powershell:

powershell -Command "Remove-Item 'c:\destination\*' -Recurse -Force"

Solution 5 - Windows

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^"

Solution 6 - Windows

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 "path\to\folder with spaces"
mydel.bat path\to\file_or_folder

Solution 7 - Windows

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_UsedByScripts\EmptyFolder" "c:\temp\MyDirectoryToEmpty" /MIR /r:3

Solution 8 - Windows

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.

Solution 9 - Windows

Navigate to the parent directory

Line1 pushd "Parent Directory"

Delete the sub folders

Line2 rd /s /q . 2>nul

https://superuser.com/questions/173859/how-can-i-delete-all-files-subfolders-in-a-given-folder-via-the-command-prompt

Solution 10 - Windows

It takes 2 simple steps. [/q means quiet, /f means forced, /s means subdir]

  1. Empty out the directory to remove

    del *.* /f/s/q  
    
  2. Remove the directory

    cd ..
    rmdir dir_name /q/s
    

See picture

Solution 11 - Windows

try this, this will search all MyFolder under root dir and delete all folders named MyFolder

for /d /r "C:\Users\test" %%a in (MyFolder\) do if exist "%%a" rmdir /s /q "%%a"

Solution 12 - Windows

del .\*

This Command delete all files & folders from current navigation in your command line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionufukgunView Question on Stackoverflow
Solution 1 - WindowsJoeyView Answer on Stackoverflow
Solution 2 - WindowsSeanView Answer on Stackoverflow
Solution 3 - WindowsBananView Answer on Stackoverflow
Solution 4 - WindowsRosberg LinharesView Answer on Stackoverflow
Solution 5 - WindowsfractorView Answer on Stackoverflow
Solution 6 - WindowsMaxim SuslovView Answer on Stackoverflow
Solution 7 - WindowsBateTechView Answer on Stackoverflow
Solution 8 - WindowsYnotincView Answer on Stackoverflow
Solution 9 - WindowsNoWarView Answer on Stackoverflow
Solution 10 - WindowsJenna LeafView Answer on Stackoverflow
Solution 11 - WindowsShailesh TiwariView Answer on Stackoverflow
Solution 12 - WindowsYuvraj HingerView Answer on Stackoverflow