How to Delete a single file using the Command Prompt on Windows
If the file gets deleted successfully, then you won’t receive any error.
If the folder gets deleted successfully, then you won’t receive any error.
Windows Command Prompt Overview
What is Command Prompt? It is a command-line interpreter application also known as cmd.exe or cmd. It can only be used in Windows operating system, and it is a gorgeous tool that can help you do a lot of advanced operations and solve some Windows issues.
Command Prompt is functional, you can deal with USB virus remove with cmd or activate Windows using cmd, and you can use it to delete files and folders very fast with the del command and rmdir command, especially if you have several items to erase. We will teach you how to use cmd delete files with a detailed tutorial.
First, you can check a video to help understand how to use Command Prompt to delete files, and we have listed the essential moments:
On a Windows computer, you might want to delete files to free up disk space, or because you don’t want the file(s) on your computer anymore.
In this article, I will show you how to force delete a file with the command prompt so you can get rid of a stubborn, unwanted file.
Step 1: Open the Command Prompt by clicking on Start (or by hitting the Windows logo key on your keyboard), searching for “cmd”, then hitting Enter:
Step 2: Head over to the folder containing the file, click on the folder address bar, and copy the address:
Step 3: In the Command Prompt, type del, right-click to the paste in the folder address, and append the filename with its extension (.html, .txt, .py, and so on).
Step 4: Hit ENTER to run the command. Then check the folder again and you shouldn’t see the file anymore:
Conclusion
The del command will delete a file even if it’s open in another program, with the exception of Office programs like MS Word.
So if you still find it hard to force delete a file, make sure it’s not open in another program, especially an Office program.
Thank you for reading.
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
The CMD del command is used to delete files from the command line in the Windows operating system.
We can delete multiple files at once:
del file1.txt file2.txt
Notes
Remove the file file1.txt in the current directory:
Remove the file file1.txt in the c:data directory:
del /f file1.txt
Remove all files in the c:data directory:
del /q *
Remove all text files (files with .txt extension) in the current directory:
Delete all files with the pattern file.* (e.g., file1.txt, file2.txt, file1.doc, etc.) in the c:data directory:
When using the wildcard character, the del command prompts for confirmation by default. You can use the Q option to suppress the confirmation message.
del /s /q C:data*
However, the del command does not remove subdirectories, only files inside subdirectories.
delete all files in a directory
The del command does not delete hidden files by default. To include hidden files, use the /a:h switch:
del /a:h file1.txt
del /a:h *
The /a switch is used to delete files based on their attributes (H represents hidden files). To see a list of all options, type del /?.
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?
asked Aug 9, 2010 at 16:42
28 gold badges86 silver badges115 bronze badges
You can do this using del and the /S flag (to tell it to remove all files from all subdirectories):
del /S C:Path odirectory*
10 gold badges104 silver badges149 bronze badges
answered Aug 9, 2010 at 16:46
del * /S /Q
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.
answered Oct 5, 2013 at 4:44
3 silver badges2 bronze badges
Navigate to the parent directory:
pushd “Parent Directory”
Delete the sub folders:
34 gold badges98 silver badges165 bronze badges
answered Jul 3, 2014 at 12:38
2 silver badges2 bronze badges
rmdir “c:pathofyourdirectory” /q /s
Don’t forget to use the quotes and for the /q /s it will delete all the repositories and without prompting.
11 gold badges51 silver badges78 bronze badges
answered Jul 31, 2013 at 18:23
1 silver badge1 bronze badge
You can do it quickly and easily by putting these three instructions in your bat file:
mkdir empty_folder
robocopy /mir empty_folder “path_to_directory”
rmdir empty_folder
answered Feb 2, 2017 at 19:20
2 gold badges5 silver badges14 bronze badges
To be clear, rd /s /q c:oobar 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.
dt.bat (or dt.cmd for the kids; whatever, I’m old, I use .bat 🤷):
Here’s how it works:
(If you like, you can comment the script with the above descriptions using rem or ::)
answered Feb 17, 2020 at 22:41
36 gold badges223 silver badges356 bronze badges
you can use rmdir to delete the files and subfolder, like this:
rmdir /s/q MyFolderPath
However, it is significantly faster, especially when you have a lot of subfolders in your structure to use del before the rmdir, like this:
answered Apr 27, 2015 at 7:21
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:
empty_my_folder.bat “C:whateverismy folder”
answered Feb 5, 2014 at 16:39
To delete 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:
You can create a script to delete whatever you want (folder or file) like this mydel.bat:
Few example of usage:
mydel.bat “path oolder with spaces”
mydel.bat path oile_or_folder
answered Nov 10, 2016 at 17:16
To delete all subdirectories and their contents use robocopy. Create an empty directory, for example C:Empty. Let’s say you want to empty C: est which has lots of subdirectories and files and more subdirectories and more files:
robocopy c:empty c: est /purge
then, rd C: est if need be.
answered May 18, 2021 at 9:31
This worked better for me when I had spaces in the folder names.
answered Feb 13, 2014 at 18:06
What most people will want
del /f /s /q “C:somePath*.*”
rmdir /s /q “C:somePath”
That will completely remove “C:somePath” and all of its contents
answered Jan 22, 2021 at 8:03
None of the answers already posted here is very good, so I will add my own answer.
for /f “delims=” %i in (‘dir path oolder /s /b /a:-d’) do del “%i” /f /q /s
fot /f “delims=” %i in (‘dir path oolder /s /b /a:d’) do rd “%i” /q /s
This should do it.
answered Jan 22, 2021 at 14:28
6 gold badges27 silver badges62 bronze badges
Seems everyone is missing the fact that we’re wanting to delete multiple sub folders, but NOT delete the parent folder. We may also no know all the names of the subfolders, and don’t want to do each one individually.
So, thinking outside the box, this is how I solved this issue.
Robocopy /Purge c:EmptyFolderToBeDeletedSoon c:FolderIWantEmpty
Make a temp directory that’s empty.
Use the RoboCopy command with the /Purge switch (/PURGE :: delete dest files/dirs that no longer exist in source.) using the empty folder as the source, and the folder we want empty as the destination.
Delete the empty temp folder we created to be the empty source for Robocopy.
Now, you have an empty folder of all files and folders, which is what this whole string was about.
answered Aug 15, 2022 at 20:07
This is what worked for me.
8 gold badges24 silver badges37 bronze badges
answered May 7, 2018 at 18:43
Example: Delete everything (folders/subfolders/files) in 3D Objects folder but want to leave 3D Objects folder alone
When CMD is oriented to working directory, using RMDIR will delete all folders, subfolders and files from the working directory. Seems like CMD process cannot process itself just like ‘I can’t throw myself into rubbish bin because the rubbish bin need to be seal by someone’
answered Oct 1, 2020 at 13:36
Here’s a two-line solution I just came up with, possibly exploiting a bug or unexpected behavior in robocopy. This works with the newest version of cmd and robocopy on Windows 10 at this writing.
It mirror syncs an empty sub-folder to its parent folder. In other words, it tells the parent folder to have all the same files as the sub-folder: none. Amusingly, this means it also deletes the empty sub-folder that it is instructed to sync with.
mkdir %TEMP%i_like_cheez
robocopy /mir %TEMP%i_like_cheez %TEMP%
answered Jan 30, 2021 at 1:44
3 silver badges12 bronze badges
this script works with folders with a space in the name
for /f “tokens=*” %%i in (‘dir /b /s /a:d “%~1″‘) do rd /S /Q “%%~i”
answered Feb 20, 2021 at 22:40
Command Prompt Delete File FAQs
We have listed some further questions and answers here:
What is delete command in cmd?
You will need the del command and rmdir command to delete files and folders with Command Prompt. And the del command is the most common command to erase one or multiple files.
How do I delete corrupted files in cmd?
When you find you can’t commonly delete corrupted files, you can try deleting files with cmd.
How do I delete multiple files in command prompt?
You can delete a single file and multiple files with the del command.
How delete all files and folders using cmd?
You can use cmd to delete all the files and folders using del and the /S flag ( to tell cmd to remove all files from all subdirectories).
What the OP asked for
del /f /s /q “C:somePath*.*”
rmdir /s /q “C:somePath”
mkdir “C:somePath”
That will remove all files and folders in and including the directory of “C:somePath” but remakes the top directory at the end.
How to Use CMD Delete File Step by Step
First, you need to know that using cmd to delete files is not like putting files and folders into Windows Recycle Bin. You can recover files from Recycle Bin folder directly, but it is not easy to recover cmd deleted files without third-party recovery software. So be careful when you are deleting files with cmd. Make sure you delete the files you don’t want anymore. Now let us learn how to open cmd first.
How to Open Command Prompt on Windows 11/10
We will teach you the easiest way to open the Command Prompt on Windows.
Step 1. Click “Start”, and you’ll see the search box.
Step 2. Type in cmd.
Step 4. Now you can use Command Prompt to delete files.
How to Use CMD Delete File with del
Step 1. Type in del with a space after it.
Step 2. Then type the path for each file you want to delete, and remember to add spaces to separate each file name.
Step 3. Make sure you type in the right path and press the Enter key.
How to Use CMD Delete Folders with rmdir
Step 1. Type in rmdir with a space after it.
Step 2. Then type the path for each folder you want to delete.
Recover Deleted Files/Folders from CMD with the File Recovery Tool
It is possible that you accidentally delete key files and folders, and you don’t know how to recover permanently deleted files. You can easily return these essential items with EaseUS Data Recovery Wizard.
You don’t have to worry about how to recover deleted videos, photos, music, emails, and documents. EaseUS Data Recovery Wizard can ensure you don’t bother with data loss anymore.
It is also a handy tool, and you can recover deleted files and folders in three steps.
Step 1. Run EaseUS Data Recovery Wizard. Choose the drive where you lost files and start scanning. This software allows you to recover lost data from all devices, including HDD, SSD, USB drive, SD card, pen drive, camera, etc.
Step 2. Browse the full scan results. You can choose the file type you need by clicking on the file type filter. EaseUS data recovery software supports 1000+ file types like photos, videos, documents, emails, audio files, and many more.
Step 3. Select the files you want to preview. Click “Recover” and set a storage location to save the recovered data.
If you wanted to empty the folder, my take is
Except for deleting files with Command Prompt, there are many other things cmd can do on Windows. For example, if you can’t find the file and find it is hidden. It is possible to show hidden files using cmd.
When you are using the computer or laptop, if you want to get back deleted videos, photos, or files, download EaseUS Data Recovery Wizard immediately, and you can find your lost files in one click.
Simple as that!
I hope I helped you out!
I recommend you to take the whole code, if you don’t want to take the whole code, then you can simplify this with.
IF NOT EXIST *path here* GOTO NOWINDIR
rmdir *path here*
mkdir *path here*
:NOWINDIR
mkdir *path here*
EDIT:
rmdir won’t work if it isn’t empty. To fix that.
IF NOT EXIST *path here* GOTO NOWINDIR
del *path here*/* /S /Q (dont copy this, the above prevents the del command from deleting everything in the folder, this is simallar to another answer.)
rmdir *path here*
mkdir *path here*
:NOWINDIR
mkdir *path here*
sdelete -s -p *path here*/*
answered Jul 30, 2020 at 8:56