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.

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.)

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

  • Windows command Line

  • Linux users

  • Open File Explorer.

  • Browse to the folder containing the files.

  • Click the Type column heading to sort all files by the type of files.

  • Highlight all the files you want to keep by clicking the first file type, hold down Shift, and click the last file.

  • Once all the files you want to keep are highlighted, on the Home Tab, click Invert Selection to select all other files and folders.

  • Once the files are highlighted that you do not want to keep, press Delete on the keyboard to delete the highlighted files.

  • How to select or highlight multiple files and folders.

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.

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

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.

  • 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.

If you want to place this command in a batch file, change %F to %%F in the line.

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.
  • How to delete a file, directory, or folder.
  • See the delete, directory, and folder definitions for further information and related links.
  • Computer file help and support.