How do i delete the contents of a folder?
Show older comments
could anyone tell me how its done please? I have a number of figures saved in the folder which i want to delete using Matlab. Thank you. the figures are named as figure1 , figure2 and so on. the number of figures i have in my folder changes everytime.
Accepted Answer
More Answers (5)
Damiano Schirinzi
on 21 Mar 2018
Another quicker solution could be to just use:
delete('PathToFolder\*')
This will delete all the files in the directory specified by PathToFolder, where PathToFolder could be either the absolute or relative path.
If you just want to delete figures with know extension of you could use:
delete('PathToFolder\*.fig')
This will delete all figures in the directory specified by PathToFolder.
Or you could use:
delete('PathToFolder\figure*')
This will delete all files with names starting with figure in the directory specified by PathToFolder.
3 Comments
Palash Sarate
on 27 Jan 2021
this seems to be less brute force. Thanks.
Renan Deuter
on 3 Feb 2021
This doesn't work for me. I suggest it's a problem with the permission windows gives Matlab. Do you know where I can check this?
Jan
on 4 Feb 2021
Please explain "does not work" with any details: Do you get an error message? Does the result differ from your expectations?
Walter Roberson
on 11 Jun 2017
Edited: Jan
on 17 Dec 2018
If you want to delete the folder itself along with everything inside it, then you can use rmdir with the 's' option.
rmdir('directory_I_dont_want', 's')
If you want the directory to be kept, then
which_dir = 'directory_to_delete_files_from';
dinfo = dir(which_dir);
dinfo([dinfo.isdir]) = []; %skip directories
filenames = fullfile(which_dir, {dinfo.name});
delete( filenames{:} )
Note: this will not delete any subdirectories or their contents: I exclude them specifically, but delete would refuse to delete them anyhow.
To be honest, what I would probably actually do is
!rm directory_to_delete_files_from/*
but I am using a Unix system and "rm" is not an MS Windows command.
3 Comments
shru s
on 11 Jun 2017
Kazem
on 5 May 2025
Instead of cmplex codes, If you want the directory to be kept
rmdir('directory_I_dont_want','s');
mkdir('directory_I_dont_want')
Walter Roberson
on 5 May 2025
That approach mostly works, but has the side effect of resetting the associated dates and access rights on the directory. That might potentially be important (but might not be either.)
Ajay Kumar
on 24 Nov 2023
Edited: Ajay Kumar
on 24 Nov 2023
2 votes
I have written a simple function for clearing the contents of a folder, please check it out: cleardir. Thanks!
1 Comment
Markus
on 26 Jan 2024
Thx, does exactly what I needed.
Pit Hoffmann
on 9 Oct 2020
Not sure if anyone needs to delete a folder with subfolders and contents in several of these folders. Here is a small function using rmdir() to delete the hole folder. It's very important to close all data of the folder to delete.
function removeFolderWithContent(path)
% Remove folder and its complete content.
% All data of the folder has to be closed.
%
% path -> path to folder as string
%% get folder content
content = dir(path);
for iContent = 3 : numel(content)
if ~content(iContent).isdir
%% remove files of folder
delete(sprintf('%s\\%s',path,content(iContent).name));
else
%% remove subfolder
removeFolderWithContent(sprintf('%s\\%s',path,content(iContent).name));
end
end
rmdir(path);
end
5 Comments
Jürgen
on 27 Jan 2021
Would it be possible for Mathworks to create a warning-indication in the IDE-Editor, if one writes code that will shadow something build-in? Something like testing ‘which’ for every assignment in the IDE-background. For functions with brackets there is some precaution in the active scope causing an error… . But for functions without bracket it can survive undetected.
What is best practice to deal and avoid shadowing?
Image Analyst
on 27 Jan 2021
That would be a nice idea, if you mean if your functions are shadowed, because lots of built-in toolbox functions have multiple versions and that's ok. For example
>> which -all find
Jan
on 28 Jan 2021
@Jürgen: See:
- https://www.mathworks.com/matlabcentral/fileexchange/27853-checkvars
- https://www.mathworks.com/matlabcentral/fileexchange/27861-uniquefuncnames
Walter Roberson
on 5 May 2025
for iContent = 3 : numel(content)
That code is based upon the mistaken idea that the directories . and .. will be reported in the first two entries of the results. That idea is mistaken on every operating system that MATLAB has ever been supported on.
You should instead use
content = dir(path);
content(ismember({content.name}, {'.', '..'})) = [];
for iContent = 1 : numel(content)
sarah
on 19 Apr 2025
0 votes
This worked for me:
delete('directory_to_delete_files_from\*')
it deletes all files in the directory but keeps the directory (empty).
Categories
Find more on Filename Construction 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!