Force delete directory in php

Sometimes you would need to remove a file or a folder from the system. To do so using SSH, you would need to execute the appropriate command – rm.

The command in its simplest form looks like:

rm myFile.txt myFile1.txt myFile2.txt

However, listing all files/folders that need to be deleted can be quite time-consuming. Fortunately, rm accepts several arguments which can ease your task. In the above example, you could type:

rm myFile*.txt

This will match all files starting with ‘myFile’ and ending in ‘.txt’ and delete them.

To delete a whole folder and its content recursively, you can use:

rm -rf foldername/

To delete all files/folders in the current directory, without deleting the directory itself, you would need to use:

rm -rf *

rmdir is a command in windows to force remove directories.  The equivalent command in Linux and Unix  is –  rm , the  command is used to remove file and directories. Learn more about using rm command in Linux to force remove directories .


By default rm only removes files and to remove directories you have to specify -r , recursive option to rm command.

interactive mode is on by default so for every deletion of file and directory it prompts you to conform if you want to delete the file or directories. Interactive mode works for few files or directories but if you have lots of files and directories you have to your rmdir force option for force removing the directory without prompting for confirmation.

rm command with -f , force option combined with -r as rm -rf option is used to force remove Linux directories.

-r stands for recursive so that rm can remove all the sub-directories also. -r option is needed to remove a directory even if the directory is empty with no subdirectory or file in it.

-f option forces remove Linux directories and it does not prompts for the delete confirmation.

Example :

to force remove all files and sub-directories under  /home/james/logs , including log directory

$rm -rf   /home/james/logs

if you want to retail the log directory and delete everything under it

$rm -rf   /home/james/logs/*

Other Options for rm command in Linux

rm -r  is same as rm -ri to remove Linux directories interactively, -i is default rm option. This is a safety check as in Linux and Unix system once a file i deleted it can not be recovered except from a backup.

-v       is the verbose option, by default rm command does not print any message about what is getting deleted, with -v option it gives message about files or directories it is deleting.

A simple method for recursively deleting a directory and all its content in PHP:-

function removeDirectory[$path] {

	$files = glob[$path . '/*'];
	foreach [$files as $file] {
		is_dir[$file] ? removeDirectory[$file] : unlink[$file];
	}
	rmdir[$path];

	return;
}

To use simply pass the path to the directory you want to remove:-

removeDirectory['/path/to/directory'];

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In PHP if you want to delete the file or directory then keep one thing in mind that you cannot delete the file or directory directly there is a condition on that i.e. there are some security issues are there so the best way to do this is you first have to delete the data present in the file or the sub files or directories present in it . Then only you are able to delete the directory . And after deleting the sub directories or files use the rmdir function to delete the main directory.

    PHP function to delete all files: In the following code, first passing the path of directory which need to delete. It checks whether the file or directory which need to delete is actually present/exist or not. If it does exist then it will open the file check whether there is something in that file or not. If not then delete the directory using rmdir directory. But if any other files are present in the directory if then it will delete the files using unlink function except the . and .. files which means the system files. After deleting all the stuff just use the rmdir function to delete the directory completely.

    Example:

    Description:

    • In this above example, deleting all files from a folder called “temp”.
    • List the files in this directory by using PHP glob function. The glob function basically “finds pathnames that match a certain pattern”. In this case, use a wildcard * [asterix] to select everything that is in the “temp” folder.
    • The glob function returns an array of file names that are in the specified folder.
    • Loop through this array.
    • Using the is_file function to check if it is a file and not a parent directory or a sub-directory.
    • Finally, use the unlink function, which deletes the file [if PHP has valid permissions – if not, an E_WARNING error will be thrown and the function will return a boolean FALSE value].
    • Delete all files and sub-directories in a directory: To delete all files and directories in all sub-directories, we can use recursion. Here is an example of a recursive PHP function that deletes every file and folder in a specified directory.

      Example:

      Output:

      • Before removing the directory:
      • After removing the directory: The directory is completely deleted.

      The function checks if the $str variable represents a path to a file then it deletes the file using the function unlink. However, if $str represents a directory, then it gets a list of all files in said directory before deleting each one. Finally, it removes the sub-directory itself by using PHP rmdir function.


    How do I empty a directory in PHP?

    The rmdir[] function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory.

    How do I force delete a directory in terminal?

    To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

    How do I delete a directory that is not empty?

    If the directory is not empty or you do not have permission to delete it, you will see an error message. To remove a directory that is not empty, use the rm command with the -r option for recursive deletion.

    Which command will delete the directory?

    Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty [it can contain only .

    Bài mới nhất

    Chủ Đề