Main Content

rmdir

Description

rmdir folderName removes the folder folderName from the current folder. folderName must be empty. If the operation is not successful, MATLAB® throws an error to the Command Window.

example

rmdir folderName s also attempts to remove all subfolders and files in folderName, regardless of their write permissions. The result for read-only files follows the practices of the operating system.

example

rmdir(folderName,ResolveSymbolicLinks=tf) specifies whether to remove a symbolic link or its target. (since R2024b)

status = rmdir(___) removes the specified folder and returns a status of 1 if the operation is successful. Otherwise, rmdir returns 0. Warnings and errors are not thrown to the Command Window. You can use this syntax with any of the input argument combinations in the previous syntaxes.

example

[status,msg] = rmdir(___) also returns the message text for any warning or error that occurs.

example

[status,msg,msgID] = rmdir(___) also returns the message ID for any warning or error that occurs.

example

Examples

collapse all

Create the folders myproject and myproject/myfiles in the current folder, and then remove them.

mkdir myproject
mkdir myproject/myfiles

rmdir myproject/myfiles
rmdir myproject

Remove the folder myfiles, which contains the files myfile1.m and myfile2.m.

Create the folder myfiles and move the files myfile1.m and myfile2.m from the current folder into the new folder.

mkdir myfiles
movefile myfile1.m myfiles
movefile myfile2.m myfiles

Try to remove the folder myfiles using rmdir. Because the myfiles folder is not empty, the operation fails and returns a status of 0 and an error message detailing why the operation failed.

[status, message, messageid] = rmdir('myfiles')
status = logical
   0

message = 
''myfiles' was not removed. The directory must be empty before removing.'
messageid = 
'MATLAB:RMDIR:DirectoryNotRemoved'

Now, use the 's' flag to remove the folder myfiles. A status of 1 and an empty message and messageid indicate that the operation is successful.

[status, message, messageid] = rmdir('myfiles', 's')
status = logical
   1

message =

  0x0 empty char array


messageid =

  0x0 empty char array

Input Arguments

collapse all

Name of folder to remove, specified as a character vector or string scalar. Specify folderName as an absolute or relative path.

Since R2024b

Remove the target of the symbolic link, specified as a numeric or logical 0 (false) or 1 (true). If tf is true, rmdir removes the target of the symbolic link. The symbolic link itself remains but directs to a deleted folder. If tf is false, rmdir removes the symbolic link itself.

Output Arguments

collapse all

Status of folder indicating if the attempt to remove the folder is successful, returned as 0 or 1. If the attempt is successful, status is 1. Otherwise, status is 0.

Data Types: logical

Error message, returned as a character vector. If an error or warning occurs, msg contains the message text of the error or warning. Otherwise, msg is empty, ''.

Error message identifier, returned as a character vector. If an error or warning occurs, msgID contains the message identifier of the error or warning. Otherwise, msgID is empty, ''.

Tips

  • As with local folders, rmdir cannot remove nonempty virtual folders unless you specify the s flag. Some file services do not support empty folders. On these services, if rmdir removes folders and leaves their parent folder empty, then the parent folder is removed as well. For more information, see Work with Remote Data.

Alternative Functionality

In the Current Folder browser, right-click the folder name and select Delete from the context menu. To open the Current Folder browser, use the Current Folder Browser command.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a

expand all