How to recursively delete all files and folders from a web server?

Good day!

There is the following problem: the site is not cleared the cache, which is necessary each time after making changes in the template and change the pictures to go to the server and clean up the folders with the caches.

Think about how to put in cron script, which I found on the Internet:
function rrmdir($dir) {
 if (is_dir($dir)) {
 $objects = scandir($dir);
 foreach ($objects as $object) {
 if ($object != "." && $object != "..") {
 if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
 echo "$dir done<br-->";
 else {
 echo "err";

$dir = dirname(__FILE__) . '/openwargame/superdir';
mkdir ("openwargame/superdir", 0755);
echo "$dir created";

the original problem is that without the crutch mkdir ("openwargame/superdir", 0755);
it removes and folder "superdir".

The question is how to do so, to delete only the entire contents of the folder "superdir"?
July 8th 19 at 12:24
4 answers
July 8th 19 at 12:26
RecursiveIteratorIterator + RecursiveDirectoryIterator

$files = new RecursiveIteratorIterator(
 new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),

foreach ($files as $fileinfo) {
 $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');

I understand that this is essentially the same, only with the use of ready-made functions? At the end of the same deleted folder "superdir" - Agustin_Schaefer commented on July 8th 19 at 12:29
: remove the last line and you will be happy. - Allene_Crona78 commented on July 8th 19 at 12:32
July 8th 19 at 12:28
If for yourself, you can, just be careful:
exec(rm-rf path/to/dir)

The rm command can delete your entire file system...
Ahead =) - Agustin_Schaefer commented on July 8th 19 at 12:31
: =) - Allene_Crona78 commented on July 8th 19 at 12:34
July 8th 19 at 12:30
And what is your crutch is bad, well, except that it is why all objects bypasses? Maybe I do not understand?
He slammed the folder is created again, much faster than recursively all around 1 object in the cache if you have 10,000 objects, this can drag on for some time, and so - SNiP, and on the tonsils.
the fact of the matter is that rmdir does not remove non-empty folder, and have to get all directories and remove files from them, and then delete the empty folders - Agustin_Schaefer commented on July 8th 19 at 12:33
rm-Rf {directory}? - Allene_Crona78 commented on July 8th 19 at 12:36
July 8th 19 at 12:32
There is already a lot of things turned, all of the case.
I'd add the following.
1) the Option "--preserve-root" to rm, just in case, suddenly who link to the root will create randomly.
2) to quickly delete a large folder - you can rename it and slowly remove.
So, there was a folder /var/cache, rename it to /var/cache_old create (or will create) a new folder /var/cache and quietly remove the old one.
By the way, Yes (= thank you - Agustin_Schaefer commented on July 8th 19 at 12:35

Find more questions by tags PHP