Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

COACHTHEM's avatar

Getting error while deleting folder and its files inside it

I am trying to delete a folder along with files inside it. below is my code

Storage::disk('public')->deleteDirectory($path);

It works fine on my local but when the same is executed on the server it is throwing below error.

[2020-03-10 09:25:02] development.ERROR: rmdir(/var/www/vhosts/trymy.sipcall.ch/storage/app/public/temp/11720_intro): Directory not empty {"userId":40758,"exception":"[object] (ErrorException(code: 0): rmdir(/var/www/vhosts/trymy.sipcall.ch/storage/app/public/temp/11720_intro): Directory not empty at /var/www/vhosts/trymy.sipcall.ch/vendor/league/flysystem/src/Adapter/Local.php:419) [stacktrace] #0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'rmdir(/var/www/...', '/var/www/vhosts...', 419, Array) #1 /var/www/vhosts/trymy.sipcall.ch/vendor/league/flysystem/src/Adapter/Local.php(419): rmdir('/var/www/vhosts...') #2 /var/www/vhosts/trymy.sipcall.ch/vendor/league/flysystem/src/Filesystem.php(251): League\Flysystem\Adapter\Local->deleteDir('temp/11720_intr...') #3 /var/www/vhosts/trymy.sipcall.ch/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(604): League\Flysystem\Filesystem->deleteDir('temp/11720_intr...') #4 /var/www/vhosts/trymy.sipcall.ch/app/Models/IvrPortaHelper.php(1543): Illuminate\Filesystem\FilesystemAdapter->deleteDirectory('temp/11720_intr...')

0 likes
6 replies
Snapey's avatar

The problem is explained in the error, you cannot delete a folder that is not empty

What does the folder contain?

COACHTHEM's avatar

it contains audio files.. but the same is working fine on my local even if the folder has files in it or if it is empty.

Also I tried deleting the files inside the folder first and then delete the folder but did not work .. Below is what I tried.

if( strlen(trim($path)) && \Storage::disk($disk)->exists($path) ){

			#DELETE ALL FILES INSIDE FILES
			$dir = \Storage::disk($disk)->files($path);
			if( is_array($dir) && count($dir) ){
				foreach($dir as $f){
		            if (\Storage::disk($disk)->exists($f)) {
		            	$ext = substr($f, strrpos($f, '.')+1);
		            	if(in_array($ext, ['.snd', '.mp3', '.wav', '.ogg'])){
		            		\Storage::disk($disk)->delete($f);
		            	}
		            }
				}
			}

			#WAIT FOR 1 SECOND BEFORE DELETING FOLDER.
			sleep(2);

			#DELETE FOLDER.
			Storage::disk($disk)->deleteDirectory($path);

		}
Snapey's avatar

are you dealing with different operating systems? Windows locally might behave differently to linux

were all the files deleted by your script?

Snapey's avatar

Just looking at flysystem, it says it will delete files, so perhaps its a permissions thing. Who owns the files in the folder?

COACHTHEM's avatar

I am using ubuntu Ubuntu 18.04.2 LTS both locally as well as on the live server. The folder seems to have all right.

COACHTHEM's avatar

Hi @snapey

It looks like the folder is being created with 755 and it seems user mapped by nfs server.

Is it possible to create the folder/files with 777 permission ?

something like this

Storage::disk('public')->put($path, $response, 777);

Please or to participate in this conversation.