There are lot of useful inbuilt functions that comes with PHP interpreter. One such function is rmdir which allows us to delete a folder/directory through PHP code.
As useful this function is, it fails to delete a directory when the directory is not empty throwing a warning message Directory not empty. As a workaround, we need to first delete all files and folders recursively inside the specified directory and then delete the directory itself.
Here, we are passing RecursiveIteratorIterator::CHILD_FIRST as mode to get the children of the directory before getting the directory name itself.