MQL4 Reference File Functions FolderDelete


The function removes the specified directory. If the folder is not empty, then it can't be removed.

bool  FolderDelete(
   string  folder_name,       // String with the name of the folder to delete
   int     common_flag=0      // Scope



[in] The name of the directory you want to delete. Contains the full path to the folder.


[in] Flag determining the location of the directory. If common_flag=FILE_COMMON, then the directory is in the shared folder for all client terminals \Terminal\Common\Files. Otherwise, the directory is in a local folder (MQL4\Files or MQL4\Tester\Files in the case of testing).

Return Value

Returns true if successful, otherwise false.


For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means, cannot be outside the file sandbox.

If the directory contains at least one file and/or subdirectory, then this directory can't be deleted, it must be cleared first. FolderClean() is used to clear a folder of all its files or subfolders.


//|                                            Demo_FolderDelete.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
//--- Description
#property description "The script shows a sample use of FolderDelete()."
#property description "First two folders are created; one of them is empty, the second one contains a file."
#property description "When trying to delete a non-empty folder, an error is returned and a warning is shown."
//--- Show the dialog of input parameters when starting the script
#property script_show_inputs
//--- Input parameters
input string   firstFolder="empty";    // An empty folder
input string   secondFolder="nonempty";// The folder, in which one file will be created
string filename="delete_me.txt";       // The name of the file that will be created in folder secondFolder
//| Script program start function                                    |
void OnStart()
//--- Write the file handle here
   int handle;
//--- Find out in what folder we are working
   string working_folder=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files";
//--- A debug message   
//--- Trying to create an empty folder relative to path MQL4\Files
   if(FolderCreate(firstFolder,0)) // 0 means that we are working in the local folder of the terminal
      //--- Enter the full path to the created folder
      PrintFormat("Folder %s has been created",working_folder+"\\"+firstFolder);
      //--- Reset the error code
      PrintFormat("Failed to create folder %s. Error code %d",working_folder+"\\"+firstFolderGetLastError());
//--- Now create a non-empty folder using the FileOpen() function
   string filepath=secondFolder+"\\"+filename;  // Form path to file that we want to open to write in a nonexistent folder
   handle=FileOpen(filepath,FILE_WRITE|FILE_TXT); // Flag FILE_WRITE in this case is obligatory, see Help for FileOpen
      PrintFormat("File %s has been opened for reading",working_folder+"\\"+filepath);
      PrintFormat("Failed to create file %s in folder %s. Error code=",filename,secondFolderGetLastError());
   Comment(StringFormat("Prepare to delete folders %s and %s"firstFoldersecondFolder));
//--- A small pause of 5 seconds to read a message in the chart
   Sleep(5000); // Sleep() cannot be used in indicators!
//--- Show a dialog and ask the user
   int choice=MessageBox(StringFormat("Do you want to delete folders %s and %s?"firstFoldersecondFolder),
                         "Deleting folders",
                         MB_YESNO|MB_ICONQUESTION); //  Two buttons - "Yes" and "No"
//--- Run an action depending on the selected variant
      //--- Delete the comment form the chart
      //--- Add a message into the "Experts" journal
      PrintFormat("Trying to delete folders %s and %s",firstFoldersecondFolder);
      //--- Delete the empty folder
         //--- The following message should appear since the folder is empty
         PrintFormat("Folder %s has been successfully deleted",firstFolder);
         PrintFormat("Failed to delete folder %s. Error code=%d"firstFolderGetLastError());
      //--- Delete the folder that contains a file 
         PrintFormat("Folder %s has been successfully deleted"secondFolder);
         //--- The following message should appear since the folder contains a file
         PrintFormat("Failed to delete folder %s. Error code=%d"secondFolderGetLastError());
      Print("Deletion canceled");

See also

FileOpen(), FolderClean(), FileMove()