- What’s the fastest way to delete a large folder in Windows?
- 6 Answers 6
- Using Windows Command Prompt:
- Using Powershell:
- Delete all files from a folder and its sub folders
- 14 Answers 14
- Using PowerShell commnads to delete a file
- Using PowerShell commnads to delete all files
- Using PowerShell commnads to delete all files and folders
- Using PowerShell command to delete all files forcefully
- How can I delete all files/subfolders in a given folder via the command prompt?
- 17 Answers 17
- How to delete files/subfolders in a specific directory at the command prompt in Windows
- 15 Answers 15
What’s the fastest way to delete a large folder in Windows?
I want to delete a folder that contains thousands of files and folders. If I use Windows Explorer to delete the folder it can take 10-15 minutes (not always, but often). Is there a faster way in Windows to delete folders?
- I don’t care about the recycle bin.
- It’s an NTFS drive.
6 Answers 6
Using Windows Command Prompt:
Using Powershell:
Note that in more cases del and rmdir wil leave you with leftover files, where Powershell manages to delete the files.
The worst way is to send to Recycle Bin: you still need to delete them. Next worst is shift+delete with Windows Explorer: it wastes loads of time checking the contents before starting deleting anything.
Next best is to use rmdir /s/q foldername from the command line. del /f/s/q foldername is good too, but it leaves behind the directory structure.
The best I’ve found is a two line batch file with a first pass to delete files and outputs to nul to avoid the overhead of writing to screen for every singe file. A second pass then cleans up the remaining directory structure:
This is nearly three times faster than a single rmdir, based on time tests with a Windows XP encrypted disk, deleting
30GB/1,000,000 files/15,000 folders: rmdir takes
2.5 hours, del+rmdir takes
53 minutes. More info at Super User.
This is a regular task for me, so I usually move the stuff I need to delete to C:\stufftodelete and have those del+rmdir commands in a deletestuff.bat batch file. This is scheduled to run at night, but sometimes I need to run it during the day so the quicker the better.
Technet documentation for del command can be found here. Additional info on the parameters used above:
- /f — Force (i.e. delete files even if they’re read only)
- /s — Recursive / Include Subfolders (this definition from SS64, as technet simply states «specified files», which isn’t helpful).
- /q — Quiet (i.e. do not prompt user for confirmation)
Documentation for rmdir here. Parameters are:
Delete all files from a folder and its sub folders
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?
14 Answers 14
This can be accomplished using PowerShell:
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
It appears the -File parameter may have been added after PowerShell v2. If that’s the case, then
It should do the trick for files that have an extension.
Short and sweet PowerShell. Not sure what the lowest version of PS it will work with.
You can do so with del command:
The /S switch is to delete only files recursively.
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.
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.
Using PowerShell commnads to delete a file
Remove-Item -Path «C:\dotnet-helpers\DummyfiletoDelete.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
Using PowerShell commnads to delete all files and folders
-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
How can I delete all files/subfolders in a given folder via the command prompt?
I would like to delete all files and subfolders in a batch file in Windows 7 and keep the top folder. Basically emptying the folder. What’s the command line instruction for that?
17 Answers 17
You can do this using del and the /S flag (to tell it to remove all files from all subdirectories):
The best Solution: e.g. i want to delete all files and sub-directories of parent directory lets say «C:\Users\Desktop\New folder\». The easy way is create batch file of below three commands.
rmdir /S /Q «C:\Users\Desktop\New folder\»
Here first it will clean all files in all sub-directories and then cleans all empty sub-directories. Since current working directory is parent directory i.e.»\New folder», rmdir command can’t delete this directory itself.
Navigate to the parent directory:
Delete the sub folders:
you can use rmdir to delete the files and subfolder, like this:
However, it is significantly faster, especially when you have a lot of subfolders in your structure to use del before the rmdir, like this:
Don’t forget to use the quotes and for the /q /s it will delete all the repositories and without prompting.
You can do it quickly and easily by putting these three instructions in your bat file:
If you want to delete all files in a folder, including all subfolders and not rely on some error conditions to keep the root folder intact (like I saw in another answer) you could have a batch file like this:
And then you would simply call it with:
user340956 was painfully close to the solution, but you know what they say about close…
To be clear, rd /s /q c:\foobar deletes the target directory in addition to its contents, but you don’t always want to delete the directory itself, sometimes you just want to delete its contents and leave the directory alone. The deltree command could do this, but Micrsoft, in its infinite «wisdom» removed the command and didn’t port it to Windows.
Here’s a solution that works without resorting to third-party tools. It’s probably about as simple and efficient as is possible with a command-line script instead of outright writing an actual executable. It doesn’t set any environment variables and it doesn’t use any loops. It’s also as safe as can be, with error-checking everywhere possible, and also as user-friendly as possible, with built-in docs.
dt.bat (or dt.cmd for the kids; whatever, I’m old, I use .bat 🤷):
Here’s how it works:
- It checks if a command-line argument has been passed, and prints usage information and quits if not.
- It uses pushd to save the current directory, then switch to the target directory, redirecting any errors to nul for a cleaner command-line experience (and cleaner logs).
- It checks to see if the current directory is now the same as the target directory, and prints an error message and quits if it is not. This avoids accidentally deleting the contents of the previous directory if the pushd command failed (e.g., passing an invalid directory, access-error, etc.)
- This check is case-insensitive, so it’s usually safe on Windows, but isn’t for any case-sensitive file-systems like those used by *nix systems, even under Windows.
- It doesn’t work with short-filenames (e.g. C:\Users\Bob Bobson\foobar won’t be seen as being the same as C:\Users\BobBob
1\foobar even if they actually are). It’s a slight inconvenience to have to use the non-short filename, but it’s better safe than sorry, especially since SFNs aren’t completely reliable or always predictable (and may even be disabled altogether).
- Because the target directory is the current directory, the system has an open file-handle to it, and thus it cannot actually delete it, so it remains as is, which is the desired behavior.
- Because it doesn’t try to remove the target directory until after its contents have been removed, it should now be empty (other than anything that also has open file handles).
(If you like, you can comment the script with the above descriptions using rem or :: .)
How to delete files/subfolders in a specific directory at the command prompt in Windows
Say, there is a variable called %pathtofolder% , as it makes it clear it is a full path of a folder.
I want to delete every single file and subfolder in this directory, but not the directory itself.
But, there might be an error like ‘this file/folder is already in use’. when that happens, it should just continue and skip that file/folder.
Is there some command for this?
15 Answers 15
You can use this shell script to clean up the folder and files within C:\Temp source:
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.
This silently removes the folder and all files and subfolders.
The simplest solution I can think of is removing the whole directory with
Then creating this directory again:
This will remove the folders and files and leave the folder behind.
. deletes all files and folders underneath the given directory, but not the directory itself.
. And FOR command line should be modified to for /F «eol=| delims=» %%I in (‘dir «%dir%\*» /AD /B 2^>nul’) do rd /Q /S «%dir%\%%I» because of FOR ignores directories with hidden attribute set. DIR with options /AD /B outputs all directories with just their names. BTW: dir is not a good name for an environment variable. – Mofi Jun 2 ’18 at 8:44
You’ll get an error message, tells you that the RMDIR command can’t access the current folder, thus it can’t delete it.
Update:
From this useful comment (thanks to Moritz Both), you may add && between, so RMDIR won’t run if the CD command fails (e.g. mistyped directory name):
/S: Deletes a directory tree (the specified directory and all its subdirectories, including all files).
/Q: Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)
I use Powershell
It will remove the contents of the folder, not the folder itself.
RD stands for REMOVE Directory.
/S : Delete all files and subfolders in addition to the folder itself. Use this to remove an entire folder tree.
/Q : Quiet — do not display YN confirmation
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:
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:
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.
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.
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%\Temp\CleanTest 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:\Temp\CleanTest\Subfolder1 , Subfolder1 is deleted by RD, and next POPD fails silently in making C:\Temp\CleanTest\Subfolder1 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.
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% .
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:
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 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.