The del command in cmd is used to delete one or more files and directories. It’s a built-in command that makes removing files and folders easy from the command line. In this comprehensive guide, we’ll cover everything you need to know about using the del command effectively.
What is the del command?
The del command, also known as erase or delete, is used to delete one or more files and directories in Windows. It works similarly to how deleting files and folders works in Windows Explorer, except del operates directly from the command line interface (CLI).
The del command is available in all versions of Windows, including Windows 10, Windows 8, Windows 7, Windows Vista, and Windows XP. It’s a very simple but powerful command once you understand the options available.
Basic syntax for del command
The basic syntax for using the del command is:
del [options] [files]
- del – The command itself that tells CMD you want to delete files/folders.
- [options] – Optional parameters that modify the behavior of del.
- [files] – The path and name of the file(s)/folder(s) you want to delete.
How to delete a single file
To delete a single file, specify the file name with extension after the del command. For example:
This will remove the file called file.txt from the current directory you’re running the command from.
How to delete multiple files
You can delete multiple files at once by listing each filename separated by a space. For example:
del file1.txt file2.txt file3.txt
This will delete file1.txt, file2.txt, and file3.txt all at once.
How to delete folders
To remove an entire folder, specify the folder name after del. For example:
This will delete the folder called folder1 and all its contents.
How to delete files and folders recursively
By default, del will not delete folders that still contain files or other subfolders. To delete a folder and all its contents recursively, use the /s option. For example:
del /s folder1
This will delete folder1 and all its files/subfolders recursively.
How to delete files based on wildcards
You can use wildcards like * and ? to delete files or folders matching a pattern. For example:
This will delete all files ending with .txt in the current directory.
This will delete all .exe files with 4 character names in the current folder.
How to force delete read-only files
Some files may have the read-only attribute set, preventing deletion. To force delete read-only files, use the /f option. For example:
del /f readonly.txt
This will forcibly delete readonly.txt even if it has the read-only attribute set.
How to delete without prompting
By default, the del command will prompt to confirm deletion of files. To skip confirmation and delete immediately, use the /q option. For example:
del /q *.tmp
This will delete all .tmp files quietly without prompting.
How to see what would be deleted without actually deleting
To see which files would get deleted without actually removing them, use the /p option. For example:
del /p file.txt
This will perform a trial run and list file.txt as being deleted without actually removing it.
How to delete empty folders
To delete empty folders, use the /s option to recursively traverse folders combined with the /q option to delete without prompting. For example:
del /s /q emptyfolder
This will delete the emptyfolder folder if it’s completely empty. If it contains any files or subfolders, they will not be deleted.
How to delete system and hidden files
System and hidden files will not get deleted by default. To remove them, use the /a option. For example:
del /a desktop.ini
This will delete the hidden desktop.ini system file.
How to delete files opened by another process
Files that are currently open or in use by another app cannot be deleted by default. To forcibly close the file and remove it, use the /f option. For example:
del /f inuse.txt
This will close inuse.txt used by another process and then delete it.
How to delete read-only files on restart
If you want read-only files to get deleted on next reboot instead of immediately, use the /f /q options together. For example:
del /f /q readonly.txt
This will schedule readonly.txt to be deleted after the next restart even if it has the read-only attribute set.
How to delete files in subfolders
To delete files that exist in subfolders recursively, use the /s option. For example:
del /s *.temp
This will delete all .temp files in the current folder and all its subfolders.
How to simulate deleting many files
You can simulate deleting a large number of files without actually removing them using the /l option. For example:
del /l /s /q c:\temp
This will perform a simulated delete of the c:\temp folder and all its contents quietly without actually deleting anything.
How to delete files permanently
By default, deleted files go to the Recycle Bin and can be recovered. To permanently delete files so they cannot be recovered, use the /p option. For example:
del /p secret.txt
This will permanently delete secret.txt so it cannot be recovered from the Recycle Bin.
How to delete files and folders from a different drive
To delete files and folders on a different drive than the current location, specify the full path. For example:
This will delete the folder1 folder on the C: drive from the current directory.
How to delete specific file types recursively
You can delete only files matching a certain extension recursively using wildcards. For example:
del /s c:\temp\*.tmp
This will delete all .tmp files inside c:\temp and all its subfolders.
How to exclude certain folders from deletion
To exclude specific folders from being deleted recursively, use the /x option. For example:
del /s /x c:\temp\excludefolder c:\temp
This will delete c:\temp and contents excluding the excludefolder subfolder.
How to delete older files based on date
To delete files older than a certain date, use the /d option. For example:
del /d -7/1/2023 c:\temp
This will delete files in c:\temp last modified over 7 days before 1/1/2023.
How to delete based on file size
To delete files over or under a certain size, use the /s option for recursion and /a for size. For example:
del /s /a:100k c:\temp
This will delete files larger than 100KB in c:\temp and subfolders.
How to delete files accessed before a certain date
To delete files not accessed for a number of days, use the /a option for last access date. For example:
del /a:-50 c:\temp
This will delete files in c:\temp not accessed for over 50 days.
How to delete system files and folders
To remove operating system files and folders, combine the /s and /a options. For example:
del /s /a c:\windows\temp
This will recursively delete the temp folder in Windows including system and hidden files.
The del command is a simple but powerful way to delete files and folders quickly from the command line. With the right options, you can delete multiple files at once, remove folders recursively, delete based on criteria like date and size, override read-only attributes, and permanently shred files.
Remember to be cautious when deleting files as there is no undo. Making backups is recommended before deleting anything critical. Overall, del is an essential tool for managing your files and cleaning up old clutter from the convenience of the command line.
Here is an example of using tables to visualize some common del command options:
|/s||Delete folder and contents recursively|
|/f||Force delete of read-only files|
|/q||Quiet mode, don’t ask to confirm deletes|
|/p||Prompt before deleting each file|
|/a||Delete hidden and system files|
|/x||Exclude certain folders from deletion|
|/l||Perform a simulated delete run|
This table summarizes some of the most useful del command line options when deleting files and folders from the command line.
Here are some example del commands to see the options in action:
Delete a single file:
Delete multiple files:
del file1.txt file2.txt file3.txt
Delete a folder recursively:
del /s foldername
Delete files matching a pattern:
Delete hidden/system files:
del /a desktop.ini
Delete files permanently:
del /p secretfile.txt
These are just a few examples of how flexible the del command can be for deleting files and folders on your Windows system.
The del command offers many options like:
- Ability to delete multiple files/folders at once
- Recursive deletion of folders using /s
- Forced deletion using /f
- Quiet or prompting modes
- Permanent file shredding
- Wildcard deletion
- Excluding folders from deletion
- Deleting based on criteria like size, date, attributes
Mastering the del command can help you quickly clean up files, save disk space, and automate deletion tasks using batch scripts. Overall, del is an indispensable file management tool on Windows.