- DeleteFile function (winbase.h)
- Syntax
- Parameters
- Return value
- Remarks
- Delete files from command line
- How to delete a file
- Delete files in bulk
- Delete multiple files
- Delete Read only files
- 3 Genius Ways to Delete a File in Use in Windows
- Force Deleting a File in Use in Windows
- More Related Articles:
- From Command Prompt
- Using a Third-Party Tool – Unlocker
- How to Restore a File If It Has Been Deleted Accidentally
- Your Safe & Reliable Data Recovery Software
- Video Tutorial on How to Recover Deleted Files on Windows 10/8/7 Easily?
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.
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.
3 Genius Ways to Delete a File in Use in Windows
Amy Dennis
Dec 21, 2020 • Filed to: Recover & Repair Files • Proven solutions
When working on Microsoft Windows, how many times have you seen a message telling that the particular file you are attempting to delete is already in use and that the program using the file must be stopped before Windows allows you to remove the file from the PC?
Annoying!! Isn’t it?
Well, not anymore.
Here are the three genius ways that you can use to force delete a file that is in use and Windows doesn’t allow you to remove it normally.
Force Deleting a File in Use in Windows
This method works most of the time. All you need to do is to force terminate the program that is using the file. Assuming that you already know which program is using the file that you want to remove, this is how you delete the file:
Note:В Windows 7 is used here as a reference PC.
Force terminating a process shuts down the entire program and you may lose any unsaved data that any other file that relies on the program may have. It is advisable to save all your work progress before proceeding.
1. On your desktop screen, right-click the taskbar.
2. From the context menu that appears, clickВ Start Task Manager.
3. In the Task Manager box, if not already there, go to theВ ProcessesВ tab.
4. From the displayed list of running processes, right-click the one that you know is using the file that you want to delete.
5. From the context menu, clickВ End ProcessВ orВ End Process Tree. (End process treeВ is clicked here.)
6. On the confirmation box that pops up, click theВ End process treeВ button from the bottom.
7. Once the process has been forcefully terminated, you can easily remove the file as it is no longer used until the process starts running again.
Note:В In some cases, you need to be quick in deleting the file after force terminating the process. This is because some processes are stubborn and even if you terminate them forcefully, they restart automatically after a few seconds.
More Related Articles:
From Command Prompt
This method requires you to open Command Prompt on your Windows PC. For the files used by some local processes (the processes that can be started and closed by a standard – non-administrator user account), opening Command Prompt on a standard user account would suffice. However, if the file is used by any system process, the elevated Command Prompt must be initialized. An elevated Command Prompt is the command window that has all the administrator rights and all the administrative tasks can be performed via commands in its interface.
This is what you need to do:
Note:В A Windows 7 PC is used in this example.
1. On your Windows PC, locate the folder that contains the file in use that you want to delete. (Do not enter into the folder.)
2.В Shift + right-clickВ the folder.
3. From the advanced context menu that appears, clickВ the Open command window hereВ or press theВ WВ key on the keyboard.
Note:В This method does not allow you to open an elevated command window.
Note: To start elevated Command Prompt, click the Start button, type CMD in the search box at the bottom of the Start menu, right-click CMD from the suggested programs’ list, click Run as administrator from the context menu, and on the User Account Control confirmation box, type the password for the admin account (or click Yes if Windows doesn’t ask you for the password) to provide your consent to continue. After the elevated command window opens up, you must use the DOS commands like CD and DIR to locate the directory where the file you want to delete is placed and to check the presence of the file respectively.
4. In the command window, type theВ DEL /F file nameВ command and pressВ EnterВ to force delete the file that is in use.
Note:В In the above command,В the file nameВ must be replaced by the name of the file along with its extension that you want to delete. For exampleВ del /f TestFile.txt.
WARNING!! – A file deleted using the DEL command does not move it to the Recycle Bin but deletes it permanently. Use the DEL command with caution.
5. Once the file is deleted, you can close Command Prompt by clicking theВ CloseВ button from the top-right corner of the window or by typingВ EXITВ in the command window and pressingВ Enter.
Using a Third-Party Tool – Unlocker
Even though you can use any of the two methods described above to remove a file that is in use, a third-party tool such as Unlocker makes your task extremely simple. The best part is that Unlocker is a freeware and is free to download and use. This is how Unlocker works:
Note:В A Windows 7 computer is used for reference.
1. Log on to your Windows PC.
2. Ensure that it is connected to the Internet.
3. Open any web browser of your choice and open your preferred search engine. (Google, Yahoo!, Bing, etc.)
4. Using the search engine, search for Unlocker.
5. Once found, download Unlocker’s installable file and install the program using the normal installation method, or get its portable version.
Note:В Unlocker is installed on this referenceВ PC.
6. After this, locate the file that you want to delete but is in use.
7. Right-click the file.
8. From the context menu that is displayed, clickВ Unlocker.
9. On theВ User Account ControlВ confirmation box, clickВ YesВ to provide your consent to continue launching the program.
10. On the displayed interface, from the available list of files in use, click to select the one that you want to unlock.
11. From the bottom of the interface, clickВ UnlockВ to unlock the file while leaving the corresponding application open. Alternatively, you can also click theВ Kill ProcessВ button to force terminate the running process that is responsible for engaging the file.
12. Once the file is unlocked, you can right-click the file and clickВ DeleteВ from the context menu.
13. ClickВ YesВ on theВ Delete FileВ confirmation box when/if displayed.
How to Restore a File If It Has Been Deleted Accidentally
Even if the file has been forcefully unlocked using any of the methods, it is moved to the Recycle Bin when deleted (except for when deleted using the command line). That being said, restoring the file from the Recycle Bin is simple. This is what you need to do.
1. On your Windows desktop screen, double-click the Recycle Bin’s icon.
2. Once inside theВ Recycle Bin, right-click the file that you want to restore.
3. From the context menu, clickВ Restore.
Note:В If a file has been deleted permanently (e.g. when removed with the DEL command), you need an efficient file recovery software such as RecoveritВ Data RecoveryВ to recover the file back.
Your Safe & Reliable Data Recovery Software
- Recover lost or deleted files, photos, audio, music, emails from any storage device effectively, safely and completely.
- Supports data recovery from recycle bin, hard drive, memory card, flash drive, digital camera, and camcorders.
- Supports to recover data for sudden deletion, formatting, hard drive corruption, virus attack, system crash under different situations.
Although the above-mentioned tricks work almost every time, you must take the utmost care while deleting any file that is in use. Deleting a wrong file may cause consequences like permanent or temporary system failure or data loss. To avoid this, before force deleting your files, make sure you have thoroughly verified the following:
- The file in use does not contain any important data.
- Force terminating the process to unlock the file doesn’t cause any permanent damage to its corresponding program.
- You have backed up the file before deleting it.
- You have tried (and failed) other ways to do what you want to do, and that force deleting the file is the only option left.
Video Tutorial on How to Recover Deleted Files on Windows 10/8/7 Easily?
Recent Videos from Recoverit