How to use del in cmd?

The del command in cmd is used to delete one or more files and folders in Windows. It works similarly to the rm and erase commands in other operating systems like Linux and macOS.

The del command has been around since the early days of DOS. It allows users to easily remove files and directories without having to use lower level commands. While the del command originated in DOS, it is still commonly used today in the Windows command prompt.

Some examples of using the del command include:

  • del file.txt – Deletes the file called file.txt
  • del folder – Deletes the folder called folder
  • del *.* – Deletes all files in the current directory
  • del *.tmp – Deletes all files with a .tmp extension

The del command is a simple yet powerful way to remove files and folders directly from the command line in Windows.

When to use del vs rmdir or erase

The main difference between del, rmdir and erase is what they can delete. Del can delete both files and directories, while rmdir can only delete empty directories, and erase can only delete files.

Specifically:

  • del: Deletes one or more files or directories. Can delete non-empty directories.
  • rmdir: Deletes empty directories only. Returns an error if trying to delete non-empty directories.
  • erase: Deletes one or more files. Cannot delete directories.

So in summary:

  • Use del for deleting both files and directories, empty or non-empty.
  • Use rmdir for deleting empty directories only.
  • Use erase for deleting files only.

Del is the most flexible since it can delete any type of file or folder. Rmdir and erase are more limited but safer since they can only delete their respective type.

Syntax of the del Command

The basic syntax for the del command in cmd is:

del [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

Here is an explanation of the main options that can be used with the del command:

  • /P – Prompts for confirmation before deleting each file. This is useful to avoid accidentally deleting important files.
  • /F – Forcefully deletes read-only files. Normally del cannot remove read-only files.
  • /S – Deletes specified files from the current directory and all subdirectories. Useful for recursively deleting folders.
  • /Q – Quiet mode, del will not ask if ok to delete on global wildcard
  • /A – Selects files to delete based on attributes such as read-only, archived, and hidden. The attributes go after /A in square brackets.

For more details and additional switches, refer to the Microsoft documentation on del command syntax.

Using del to delete files

The del command can be used to delete one or more files in Windows. To delete a single file, simply type ‘del’ followed by the filename:

For example: del myfile.txt

This will permanently delete myfile.txt from the current directory. The del command does not prompt for confirmation before deleting the file. So be careful when using del on important files.

To delete multiple files at once, you can specify multiple filenames separated by spaces like this:

del file1.txt file2.txt file3.txt

This will delete all 3 files listed. You can also use wildcards like ‘*’ and ‘?’ to delete files matching a pattern. For example:

del *.tmp

Will delete all files ending in .tmp in the current folder.

According to ss64.com, if a folder name is given instead of a file, all files in the folder will be deleted, but the folder itself will not be removed.

Using del to delete folders

The del command can be used to delete empty folders in Windows. To delete an empty folder, use the command:

del foldername

For example, to delete an empty folder called “temp”:

del temp

If the folder contains any files or subfolders, the del command will display an error saying it cannot delete a directory that is not empty. To delete a folder and all its contents recursively, use the /s switch:

del /s foldername

This will delete the specified folder plus all files and subfolders contained within it. For example, to delete a folder called “project” and all its contents:

del /s project

The /s switch deletes the folder tree recursively, meaning it deletes all subfolders and files within subfolders as well. This allows you to easily delete a folder and its entire contents [1].

Advanced uses of del

The del command has some advanced options that allow you to delete files in special circumstances. Two useful advanced options are deleting read-only files and excluding certain files from deletion.

To delete read-only files, you can use the /F switch. This will force the deletion of files even if they have the read-only attribute set. For example:
del /F file1.txt
This will delete file1.txt even if it is marked read-only. See Microsoft’s del documentation for more details.

You can also exclude certain files from being deleted using the /EXCLUDE switch. This is useful if you want to delete many files matching a pattern, but want to avoid deleting specific files. For instance:
del /EXCLUDE:file2.txt *.txt
This will delete all .txt files in the current directory except for file2.txt. Refer to this del tutorial for additional examples of excluding files.

Using options like /F and /EXCLUDE allows you to precisely control which files are deleted when using the del command in advanced ways.

Using del with wildcards

The del command supports using wildcards (* and ?) to match and delete multiple files or folders. The asterisk (*) matches any string of characters, while the question mark (?) matches any single character.
For example, to delete all files with a .tmp extension:

del *.tmp

To delete all files starting with temp and ending with any extension:

del temp.*

You can also use wildcards to match date ranges. For instance, to delete files modified in the last 7 days:

del *.* /D -7

However, wildcards are only resolved in the last element of the path. So to delete .tmp files in subfolders, you would need to use /S to recurse into directories:

del /S *.tmp

See this Super User answer for more details on using del with wildcards in subfolders.

Undoing a del command

In some cases, it may be possible to undo a del command and recover deleted files, but this depends on the specific situation:

If you just deleted the files and they still reside in the Recycle Bin, you can often restore them by opening the Recycle Bin and right-clicking on the file(s) to select Restore. This allows you to recover files that were recently deleted.

However, if you have emptied the Recycle Bin or deleted the files using Shift+Delete to skip the Recycle Bin, then undoing the deletion becomes more difficult. The free space where the deleted files resided is marked as available and may get overwritten with new data.

There are third party tools like Recuva that can scan the drive and recover deleted files, but recovery depends on the files not yet being overwritten. The sooner you run recovery, the greater the chances of restoring deleted files after a del command.

Trying to undelete files using the native undelete command in cmd often does not work, since it relies on the old FAT file system. The undelete command is ineffective for modern NTFS volumes. So relying on the Recycle Bin and third party tools is a better option for recovering deleted files.

Overall, it’s best not to depend on being able to undo a del command since deletion is not easily reversed in most cases. Instead, be cautious when deleting files and have backups available to restore lost data.

Common issues with del

The most common issues that arise when using the del command involve receiving “Access denied” errors or finding that files are missing after running the command. Both of these problems typically occur due to lack of permissions.

An “Access denied” error displays when you do not have permissions to delete the specified file or folder. This is common in Windows environments where permissions are restricted. To fix it, you need to run the del command as an admin or take ownership of the file/folder you want to delete.

For example:

del C:\Folder\file.txt

Access denied

Missing files after del often occur because the command deleted the files successfully, but you were expecting them not to be deleted. This can happen if you forget to include the /p option for confirmation before deletion. It can also occur when using wildcards if you are not aware of all matching files that will get deleted.

To avoid unexpectedly missing files, it’s best practice to:

  • Always use the /p switch for interactive deletion
  • Preview wildcards before executing deletion (e.g. dir /s *.*)
  • Use the /q option to disable confirmation for automated scripts

See this SuperUser post for more details on troubleshooting “Access denied” errors: Windows del command not working

Alternatives to del

While del is a common command for deleting files and folders in Windows, there are a few other options that can also be used:

rmdir

The rmdir (remove directory) command is used specifically for deleting empty directories. To delete a folder and all its contents using rmdir, you would first need to delete all the files inside the directory. According to SS64, rmdir cannot delete directories that still contain files or subfolders.

erase

The erase command is similar to del, but deletes files differently. As noted on Lifewire, erase overwrites the data in a file before deleting it, preventing the data from being recovered. This makes erase more secure for permanently deleting sensitive files.

PowerShell Remove-Item

In PowerShell, the Remove-Item cmdlet serves the same purpose as del. As Microsoft Docs explains, Remove-Item can delete files and folders recursively, allowing it to remove entire directory trees easily.

So in summary, while del is the standard command, rmdir, erase, and PowerShell provide alternatives in specific use cases like removing empty folders, overwrite deleting files, and recursive deletions.

Leave a Comment