Delete files inside subfolder
Show older comments
Hello,
I have a folder:
Testfolder
Inside are:
1.mat
1.txt
1.wav
SubTestfolder
Inside SubTestfolder are:
2.mat
2.txt
2.wav
I do
Del_path = fullfile(TestFolderPath,SubTestFoldername);
delete(Del_path,'*.mat')
I was waiting that 2.mat should be deleted, but it will be 1.mat deleted, not 2.mat. Why? How can I delete files/all files in subfolder?
Thank you!
Accepted Answer
More Answers (2)
Thomas Carpenter
on 13 Nov 2026 at 17:14
Edited: Thomas Carpenter
on 13 Nov 2026 at 17:30
A clean solution to delete all matching files recursively in subfolders matching a wildcard:
files = dir(fullfile(searchDir,'**/*.mat'))
if ~isempty(files)
files = fullfile({files.folder}, {files.name});
delete(files{:})
end
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!