How to force delete all files in directory by cmd?

Deleting files in a directory can be tedious if there are many files present. The standard delete command may not work if the files are locked or in use by a program. In such cases, you need a forced delete option to remove all files quickly. The Command Prompt in Windows provides the “del” command with a “/f” switch to force delete files.

What is the del command in CMD?

The “del” command in CMD or the Command Prompt is used to delete one or more files and directories. It works similar to how the delete option works in Windows Explorer.

Some examples of using the del command are:

  • del filename – Deletes a file named filename
  • del foldername – Deletes a folder named foldername
  • del * – Deletes all files in the current directory

When you use the del command without any switch, it will prompt for confirmation before deleting each file. You need to press Y to confirm deletion of each file.

What is the /f switch in the del command?

The “/f” switch used along with the del command forces the deletion of files and folders. It does the following when used:

  • Deletes files and folders without prompting for confirmation
  • Allows deleting locked files which are open or in use
  • Removes read-only files
  • Allows deleting hidden and system files

So the “/f” switch with del is used to force delete files even if they are locked or have various permissions set on them. This makes it useful to remove stubborn files.

Force delete all files in a folder using del /f

Follow these steps to force delete all files in a folder using the del command with the /f switch in Command Prompt:

  1. Open Command Prompt by typing cmd in the Start menu or Run dialog box.
  2. Navigate to the folder containing the files you want to delete by using the cd command. For example, to navigate to C:\MyFolder, type cd C:\MyFolder and press Enter.
  3. Type the command del /f * and press Enter. This will force delete all files in the current folder without any prompt or confirmation.
  4. If you also want to remove all subfolders in the directory along with the files, use del /f /s *. The /s switch deletes files from all subfolders as well.
  5. Check if the files have been deleted by the command by entering the dir command to list the directory contents.

This completely wipes out all files in the given folder and its subfolders. The files are deleted permanently without going to Recycle Bin.

Force delete single or multiple files

You can also use the del /f command to remove specific files and not everything in a directory. The syntax is:

del /f filename1 filename2

This will forcibly remove the two files named filename1 and filename2. You can specify any number of files to force delete.

Some examples:

  • del /f report.doc – Deletes report.doc forcefully
  • del /f *.tmp – Removes all files with .tmp extension
  • del /f file1.txt file2.txt – Deletes file1.txt and file2.txt

Force delete read-only files

Files marked as read-only cannot be deleted easily. But the /f switch overrides this and removes read-only files as well. Here is how you can delete read-only files:

  1. First check which files are marked read-only by typing dir /a. Read-only files have an R attribute.
  2. Use the command del /f filename to delete the read-only file. For example: del /f report.doc
  3. This will remove the read-only file report.doc forcefully, overriding its read-only nature.

Delete locked files using del /f

Files that are open or in use get locked by Windows and cannot be deleted normally. But the forced delete command removes locked files as well.

To delete locked files:

  1. Close any programs that could be using the file. This releases the file lock.
  2. Use del /f filename to delete the locked file forcefully.
  3. If the file is still locked, you may have to restart the computer to release the lock.
  4. After restarting, use the /f switch to delete the file.

Delete folder with files using /f and /s

If you need to remove an entire folder containing files and subfolders, use the /s switch along with /f.

The steps are:

  1. Navigate to the parent folder containing the folder you want to delete. For example, if you want to delete C:\MyData\Reports, go to C:\MyData.
  2. Use the command: del /f /s Reports
  3. This will delete the Reports folder and all its content recursively without prompting.

The /s switch deletes the folder tree forcefully. Remember to navigate out of the folder first before running this command.

Delete hidden and system files

Hidden and system files like pagefile.sys and hiberfil.sys often need admin access to delete. The /f switch lets you remove them as well.

To delete hidden or system files:

  1. Open an elevated Command Prompt as admin by searching for cmd, right-clicking it and selecting Run as administrator.
  2. Navigate to the drive containing the hidden system file.
  3. Use del /a /f filename. The /a switch shows hidden files.
  4. This will delete the hidden system file forcefully.

Delete files without Recycle Bin

Files deleted with the del command bypass the Recycle Bin. They cannot be recovered or restored from the bin. This results in permanent deletion.

To delete files permanently without Recycle Bin:

  1. Use the /f switch – del /f filename
  2. Deleted files don’t go to Recycle Bin and are wiped out instantly. Use with caution.
  3. This applies to deletion of folders as well with del /f /s foldername

Delete all files excluding some

If you want to delete all files in a folder but keep some specific files, you can use wildcards to exclude them from deletion.

For example, to delete all files except file1.txt, file2.txt and file3.txt:

del /f /s *
del /f /s !file1.txt !file2.txt !file3.txt

The first command deletes all files recursively. The second command excludes the three mentioned files using the ! character.

Delete based on date, size, attribute

The del command also allows deleting files based on criteria like date, size and attributes. Some useful examples are:

  • del /f /s /a :-1 – Delete files created today
  • del /f /s /a :d7-30 – Delete files older than 30 days
  • del /f /s /a :da-14 – Delete files older than 14 days
  • del /f /s /a :t2 – Delete files older than 2 minutes
  • del /f /s /a :s100 – Delete files smaller than 100 bytes

This provides fine control over which files to delete based on criteria.

Delete non-empty directories

By default, the del command won’t delete non-empty directories. To force delete directories containing files:

  • Navigate to the parent directory of the folder you want to delete
  • Use del /f /s foldername to delete folder and contents

This will remove the directory and all its files and subfolders forcefully.

Delete using external delete commands

Apart from the inbuilt del command, you can also use external file deletion tools like:

  • sdelete – Secure file deletion utility from Sysinternals
  • erase – Force delete utility from 1st-Secure.com
  • delprof2 – Used to delete user profiles in bulk

These tools utilize advanced techniques for secure deletion of files and folders on Windows.

Recover deleted files

If you accidentally deleted important files using the force delete command, stop using the drive immediately to prevent overwriting data.

You can recover deleted files using:

  • Data recovery software like Stellar Data Recovery, EaseUS Data Recovery, etc.
  • Restore from a backup if you have one
  • Retrieve from the Recycle Bin if you had deleted normally

But forced deletion reduces chances of recovery significantly. So use the del /f command cautiously.

Conclusion

The del command with the /f switch in Command Prompt allows you to force delete files, folders, hidden system files, locked files, read-only files, etc. It deletes without confirmation or Recycle Bin. Useful in scenarios where normal deletion does not work due to file permissions.

Make sure to backup important data before force deleting. Use the /s switch to remove folder trees completely. You can exclude files from deletion using wildcards. The command works faster than GUI delete but does not allow recovery from Recycle Bin.

Overall, the del /f command provides a powerful method to forcefully clean up your folders and drives by removing unnecessary files permanently.