Skip to content

Deleting Files Created by Webserver

Files created by the web application are usually owned by www-data, the webserver user. As a result, they cannot be deleted over FTP, because the FTP user is different. This often happens in cache and session directories. However, FTP user is typically member of the www-data group. So, to be able to delete the files over FTP, you just need to make the directories and the files within group-writable. If you cannot configure this directly in your web application, you can achieve it using a simple script.

Changing file permissions

For a single file

<?php chmod "path/to/files/file.txt", 0664); ?>

For the whole directory

<?php  
$pathname = "path/to/files";  
$filemode = 0664;  
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname));  

foreach($iterator as $item) {  
 chmod($item, $filemode);  
}  
?>

After that, you should be able to delete the files even over FTP.

Deleting files using PHP

Another option is to delete the files directly using PHP script run under www-data. A sample script is below.

<?php  
$dir = "path/to/files";  
$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);  
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);  

foreach ( $ri as $file ) {  
 $file->isDir() ? rmdir($file) : unlink(realpath($file));  
}  
return true;  
?>

In order the recursively delete the whole directory, all files and subdirectories within must be writable by the www-data user.

Conclusion

If none of the methods above have led to expected results, or the number of files is too large, please contact our technical support.