How to delete all files except a specific file extension

Updated: 12/30/2019 by Computer Hope
Delete file

It may be necessary to delete all files in a folder except files meeting specific criteria. On this page, we help with deleting all files in a directory that do not have a specific file extension.

Note

When you are using commands, scripts, or software that deletes multiple files, we strongly recommend you back up your data first. Having a verified backup is important! If you accidentally delete the wrong files, you can restore them from your backup.

Microsoft Windows

In Microsoft Windows, you can delete multiple files of a specific type or file extension by following the steps below. (You may also want to try doing it through the Windows command line.)

  1. Open File Explorer.
  2. Browse to the folder containing the files.
  3. Click the Type column heading to sort all files by the type of files.
  4. Highlight all the files you want to keep by clicking the first file type, hold down Shift, and click the last file.
  5. Once all the files you want to keep are highlighted, on the Home Tab, click Invert Selection to select all other files and folders.
  6. Once the files are highlighted that you do not want to keep, press Delete on the keyboard to delete the highlighted files.
Tip

To unselect some highlighted files, hold down Ctrl and click the files or folders you do not want to delete before pressing Delete.

Windows command Line

Deleting all files in the current Windows command line (DOS) directory, except files with a certain file extension, can best be done with the for command.

for /f %F in ('dir /b /a-d ^| findstr /vile ".tiff .jpg"') do del "%F"

In the example above, the command is deleting all files in the current directory, except files with the file extension .tiff and .jpg.

Tip

To place this command in a batch file, change %F to %%F in the line.

  • See our for command page for further information on this command.

Linux users

Deleting all files in the current Linux directory with a specific file extension can be done with the find command.

find . -type f ! -iname "*.tiff" -delete

In the example above, the command is deleting all files in the current directory, except files with the file extension .tiff.

  • See our find command page for further information on this command.