- Delete files from command line
- How to delete a file
- Delete files in bulk
- Delete multiple files
- Delete Read only files
- DeleteFile function (winbase.h)
- Syntax
- Parameters
- Return value
- Remarks
- How to Delete a File in Windows (several variants)
- Contents
- Delete file to “Recycle Bin”
- How to delete a file (folder) using the menu
- How to delete a file (folder) using the button
- Disable (enable) delete confirmation
- How to recovery files from Recycle Bin
- How to empty Recycle Bin
- How to permanent Delete
- Guaranteed file deletion
- Guaranteed deletion of data on the active disk
- Guaranteed data deletion on inactive devices
- Delete via the Command prompt
- How to delete a file via command prompt
- How to delete a folder via command prompt
- Delete via PowerShell
- Comparison table of options how to delete a file
- How To Delete Files & Folders – From The Windows Command Line (CMD, DOS)
- Jump Right In:
- Navigating To The File Using CMD.
- Deleting Folders / Directories Using CMD.
- Deleting Files Using CMD.
- Creating a dummy file with a specific size.
- Delete Many Files Or Folders At Once.
- Delete Files Or Folders From Any Directory.
- What We’ll Learn:
- Navigating To The File Using CMD:
- Deleting Folders / Directories Using CMD
- Deleting Files Using CMD
- Delete Many Files Or Folders At Once — From CMD
- Delete Files Or Folders From Any Directory (CMD)
- Summary:
- That’s It!
Delete files from command line
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!
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
To delete all the files with ‘log’ extension
Delete all files having the prefix ‘abc’
Delete all files having ‘PIC’ somewhere in the file name.
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
Delete Read only files
We can’t delete a read-only file using simple‘del’ command. We get access denied error in this scenario.
A read-only file can be deleted by adding /F flag.
Alternatively, we can use the below command too
Is there any batch command to delete all zero byte sized files from cmd.
There is a way to remove folders from the commandline, use “RD” (remove directory), and it has a couple of silent switches, “RD /S /Q C:/Directory” will remove C:/Directory silently, I use this all the time. I think that the directory path has to be in quotations if it contains any spaces.
UAC might want to fight over deleting files, in that case, the command “takeown” to give you ownership of the file first, should get around that.
Sometimes we get an error when we try to delete a File or a folder for no reason , but of course there is a reason.We have many damage file or blocked files.Do not worry if we want to remove the error files or too long path files from our system,here I suggest a smooth way.So use “Long path tool” software and keep yourself.
Well done, I used this when del *1*.*
Will keep this in my reference book. Thanks a lot!
Is there a way to exclude filenames matching a pattern from a delete? For example, I want to delete all files except those whose names are like *xyz*
I am trying to delete csv files in DOS that have a date suffix of earlier
than 2 months ago. I need to do this dynamically, from a batch file.
Is this possible ? Many thanks.
Bethany
I want to delete a file from all the user’s profiles using cmd.
The file is on the desktop.
Name of the file-Hello My Name
I’m writing del %PUBLIC%\Desktop\Hello*My*Name.pdf,but that doesnt seem to work.
I also tried del %PUBLIC%\Desktop\*Hello* but that didnt work too.
Any help is appreciated
c. Using the for loop create 5 text files which start from letter a (ex: a1, a2, a3, a4, a5).
can you help me
create 5 files like this command.
for /l %a in (1 1 5) do mkdir File%a.
DeleteFile function (winbase.h)
Deletes an existing file.
To perform this operation as a transacted operation, use the DeleteFileTransacted function.
Syntax
Parameters
The name of the file to be deleted.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend «\?» to the path. For more information, see Naming a File.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
Remarks
If an application attempts to delete a file that does not exist, the DeleteFile function fails with ERROR_FILE_NOT_FOUND. If the file is a read-only file, the function fails with ERROR_ACCESS_DENIED.
The following list identifies some tips for deleting, removing, or closing files:
- To delete a read-only file, first you must remove the read-only attribute.
- To delete or rename a file, you must have either delete permission on the file, or delete child permission in the parent directory.
- To recursively delete the files in a directory, use the SHFileOperation function.
- To remove an empty directory, use the RemoveDirectory function.
- To close an open file, use the CloseHandle function.
If you set up a directory with all access except delete and delete child, and the access control lists (ACL) of new files are inherited, then you can create a file without being able to delete it. However, then you can create a file, and then get all the access you request on the handle that is returned to you at the time you create the file.
If you request delete permission at the time you create a file, you can delete or rename the file with that handle, but not with any other handle. For more information, see File Security and Access Rights.
The DeleteFile function fails if an application attempts to delete a file that has other handles open for normal I/O or as a memory-mapped file (FILE_SHARE_DELETE must have been specified when other handles were opened).
The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.
Symbolic link behavior—
If the path points to a symbolic link, the symbolic link is deleted, not the target. To delete a target, you must call CreateFile and specify FILE_FLAG_DELETE_ON_CLOSE.
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.
How to Delete a File in Windows (several variants)
This basic guide describes in detail how to delete a file (folder) in Windows (several ways): from simple deletion to guaranteed deletion with protection from data recovery programs.
Contents
Delete file to “Recycle Bin”
💡 To select multiple files or folders, hold the special button and press the left mouse button:
- Сtrl — highlight a specific file or folder;
- Shift — selection of a range of files or folders.
How to delete a file (folder) using the menu
- Right-click on a document or folder;
- Select the Delete item;
- Click Yes in the confirmation request.
The file will be deleted to the Recycle Bin.
How to delete a file (folder) using the button
- Highlight a document;
- Press the Del key;
- Click Yes in the confirmation request.
The file will be deleted to the Recycle Bin.
Disable (enable) delete confirmation
- Click on the Recycle Bin can icon with the right mouse button;
- Select Properties;
- Disable the Display delete confirmation dialog.
💡 If you just deleted the desired file, press the key combination Ctrl + Z. This will undo your last action in Windows, the deleted file or folder will return to its place.
How to recovery files from Recycle Bin
- Open the Recycle Bin;
- Select the desired file or folder;
- Right click on the selected file;
- Select Recover.
Restore files
The file will be restored to the folder from which it was deleted.
You can transfer the file (or folder) from the Recycle Bin to the folder you need.
Click and hold the left mouse button on the selected file and drag it to the folder or Desktop you need.
How to empty Recycle Bin
You can delete all files from the Recycle Bin.
- Click on the Recycle Bin with the right mouse button;
- Click Empty Recycle Bin.
Empty Recycle Bin
How to permanent Delete
- Highlight a file or folder;
- Press the key combination Shift + Del;
- Click Yes in the confirmation request.
Permanently delete folder
Guaranteed file deletion
Guaranteed deletion of data on the active disk
For guaranteed data deletion, we use the standard CIPHER utility.
Cipher.exe is a command-line tool (included with Windows 2000) that you can use to manage encrypted data by using the Encrypting File System (EFS)
💡 To overwrite as much data as possible, it is recommended that you close all other applications while running CIPHER / W.
- Run the command prompt as administrator;
- We launch the CIPHER command with the necessary keys.
/W — key, deletes data from available unused disk space in the entire volume. The content is overwritten in three stages, with values: 00, FF and a random number.
:X — work volume label.
Utility Cipher.exe
Guaranteed data deletion on inactive devices
For guaranteed data deletion on inactive devices: unused hard disks, portable hard disks and flash devices, we use the standard FORMAT utility.
- Run the command prompt as administrator;
- We start the format command with the necessary keys.
X – specifies the drive letter (followed by a colon), mount point, or volume name.
/P:count — zero every sector on the volume. After that, the volume will be overwritten “count” times using a different random number each time. If “count” is zero, no additional overwrites are made after zeroing every sector.
To run the utility successfully, you must enter the correct label of the active volume/device.
Utility Format.com
The process can take a long time, especially if a large number is indicated in the rewrite counter.
Delete via the Command prompt
How to delete a file via command prompt
- Run the command prompt;
- Go to the desired folder (cd command);
- Run the del command and the fully qualified file name with the extension.
If there are spaces in the file name, enclose the name in quotation marks.
To delete files from system directories, you must run the command prompt as administrator.
How to delete a folder via command prompt
- Run the command prompt;
- Go to the desired folder;
- Run the RD command.
/S – removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.
If there are spaces in the directory name, enclose the name in quotation marks.
To delete directory from system directories, you must run the command prompt as administrator.
Delete via PowerShell
The PowerShell command to delete folders and files is Remove-Item.
The synonyms of the Remove-Item command are: del, erase, rd, ri, rm and rmdir, so commands from the Windows command prompt will also work.
Windows PowerShell
X – full path to the file (with extension) or folder.
Recurse – the key indicating the deletion of the content folder.
Comparison table of options how to delete a file
Comparison table on how to delete files and folders.
Deleting data to the Recycle Bin | Deleted data can be restored from the Recycle Bin folder on the Desktop. |
Permanent data deletion | Deleted data can only be restored using special programs. |
Guaranteed data deletion | Deleted data cannot be recovered even by special data recovery programs. But special services and laboratories can still recover deleted data. |
How to delete a file (folder) in Windows was discussed in this article. I hope that now you can delete files or folders in different ways, including guaranteed deletion of files beyond recovery with special programs. However, if you run into any problems, do not hesitate to write in the comments. I will try to help.
How To Delete Files & Folders – From The Windows Command Line (CMD, DOS)
Jump Right In:
Navigating To The File Using CMD.
Deleting Folders / Directories Using CMD.
Deleting Files Using CMD.
Creating a dummy file with a specific size.
Delete Many Files Or Folders At Once.
Delete Files Or Folders From Any Directory.
What We’ll Learn:
Welcome!
This guide is all about deleting files and folders from the windows command line.
- We’ll start by learning, how to navigate the file or folder we want to delete form the command prompt.
- How to use the Del and Rmdir commands to remove files and folders respectively.
- How to take advantage of wildcards to perform an operation on multiple files or directories.
- And finally, we’ll learn how to delete files and folders without navigating to the directory in which they are located.
Lets get started!
Navigating To The File Using CMD:
To delete a file or folder using the command prompt we must first navigate to the location of our file.
By default the command line is located at a folder named after your computers username (C:\Users\MyPC). This folder contains your documents, downloads and desktop directories, among others.
To view all the files in this directory use the “Dir” command, which stands for directory.
Every file in this directory will be displayed at once.
If the file or directory you want to delete is located at your desktop for example use the “Cd” command to change the command prompts directory.
Simply type “Cd” followed by the folder you want to navigate into within quotes.
You will at once navigate into the desktop directory and from here you can use the “Dir” command once again to locate the file or folder you want to delete.
If you navigated to the wrong directory by mistake, type:
You will immediately be navigated into the previous directory.
In a similar way you can use a combination of the “Dir” and “Cd” commands to navigate to any directory and locate any file or folder.
Deleting Folders / Directories Using CMD
To delete a folder use the “Rmdir” command, which stands for “Remove Directory”.
Keep in mind that as the name suggests, “Rmdir” only deletes directories. Trying to delete a file using “Rmdir” will constitute in an error.
You might be wondering why we surrounded the name of our folder in quotes. The quotes tell the command line that the spaces separating the words do not constitute an additional parameter and that they are simply a part of the name of our folder.
“Rmdir” without any parameters only removes folders that are empty, to delete a folder along with the files or folders within it you need to use the /S parameter.
The command would look like this:
After entering the command the command line will ask if you are sure you want to delete this file. Simply press “ y ” to confirm.
If you want to delete a folder in a directory other than your users folder you would need to start the command prompt as an administrator. Be careful when deleting folders with administration privileges, the wrong command can very easily break your computer.
Deleting Files Using CMD
The command for deleting files using the cmd is “Del“. Unlike “ Rmdir “, “ Del “can only delete files. Using “ Del ” to delete a folder will constitute in an error .
To delete a file simply type “Del” followed by the name of your file along with its extension in quotes.
Your file should be immediately deleted.
Once again if you file is not located in the users folder or within any of its sub-directories you would need to start the command prompt as an administrator.
Be very careful however , the wrong command in an administrator command prompt can very easily break your computer.
To delete more than one file at once simply enter all the names of your files one after the other.
Delete Many Files Or Folders At Once — From CMD
Entering the name of every file or folder you want to delete is not always practical.
Work smarter not harder.
You can delete every file that begins with a set of characters by using a wildcard.
Wildcards are special symbols that take the place of characters or words.
The most commonly used wildcard is the asterix.
Typing “Del” followed by a set of characters that is shared among the names of the files we want to delete and an asterix, will delete all of them.
Every file that begins with the word “File” will be deleted.
To delete a set of folders instead, simply replace the Del command with Rmdir.
Similarly you can delete only files of a specific type by specifying its extension before a wildcard. With the following command for example, we can delete every text file in our current directory.
We can even delete every file or folder by using a wildcard, all by itself.
Here is an example using the Rmdir command:
Every folder in your current directory will be deleted along with any contents within it.
Be careful when deleting files and folders, the wrong command can easily delete important files and folders.
Delete Files Or Folders From Any Directory (CMD)
While you can use the “Rmdir” and “Del” commands to delete files and folders in your current directory, what if you want to delete a folder or file that’s located in a different directory without having to navigate there yourself?
You would have to enter your command as usual but instead of typing the name of the file or folder you want to delete directly you would have to enter the location of the target directory followed by a backward slash and then name of your folder or file.
It should look something like this:
In the same way you can replace “Rmdir” with any command to complete any task from any directory.
Summary:
- Navigate to the directory the file you want to delete is located with the “CD” and “Dir” commands.
- Use “Rmdir” to delete folders and “Del” to delete files. Don’t forget to surround the name of your folder in quotes if it contains a space.
- Use wildcards to delete many files or folders at once.
That’s It!
You now know how to delete files and folders directly from the command prompt.
If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.