How to Remove a Directory That Is Not Empty

How to delete a non-empty folder in Batch script?

For the previous answer, I checked the current Microsoft documentation about commands it has, and it does NOT say clearly that the del command will delete only files.

The command you need to recursively delete a folder, and all files OR folders it contains is:

rmdir [name of the folder] /s /q

Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, but they come AFTER the name of the folder! This is what the command documentation shows, as you may read here.

But there are more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has system files or hidden files, the rmdir command will fail. To solve this problem, you need to do more work. To quote the documentation pointed above:

You can't delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:

The directory is not empty

Use the dir /a command to list all files (including hidden and system files). Then use the attrib command with -h to remove hidden file attributes, -s to remove system file attributes, or -h -s to remove both hidden and system file attributes. After the hidden and file attributes have been removed, you can delete the files.

How do I remove/delete a folder that is not empty?

import shutil

shutil.rmtree('/folder_name')

Standard Library Reference: shutil.rmtree.

By design, rmtree fails on folder trees containing read-only files. If you want the folder to be deleted regardless of whether it contains read-only files, then use

shutil.rmtree('/folder_name', ignore_errors=True)

Cannot remove .git/: Directory not empty

The ?????????? ? ? ? ?... output of ls may indicate that you are missing the correct permissions to access this file. If this is the problem, you could try giving yourself permissions over the parent directory and its children with the following command:

sudo chmod -R g+x .git/

How to remove a directory? Is os.removedirs and os.rmdir only used to delete empty directories?

You should be using shutil.rmtree to recursively delete directory:

import shutil
shutil.rmtree('/path/to/your/dir/')

Answer to your question:

Is os.removedirs and os.rmdir only used to delete empty directories?

Yes, they can only be used to delete empty directories.


Below is the description from official Python document which clearly stats that.

os.rmdir(path, *, dir_fd=None)

Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised. In order to remove whole directory trees, shutil.rmtree() can be used.

os.removedirs(name)

Remove directories recursively. Works like rmdir() except that, if the leaf directory is successfully removed, removedirs() tries to successively remove every parent directory mentioned in path until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, os.removedirs('foo/bar/baz') will first remove the directory 'foo/bar/baz', and then remove 'foo/bar' and 'foo' if they are empty. Raises OSError if the leaf directory could not be successfully removed.

Cannot remove item. The directory is not empty

You could try the following:

Remove-Item -Force -Recurse -Path "$directoryPath\*"

Note when using the -Recurse parameter with -Include in Remove-Item, it can be unreliable. So it's best to recurse the files first with Get-ChildItem and then pipe into Remove-Item. This may also help if you deleting large folder structures.

Get-ChildItem $directoryPath -Recurse | Remove-Item -Force   

Cannot remove .git: Directory not empty

You can use the fuser command.

The fuser is a command line utility intended to locate processes based on the files, directories, or a socket a particular process is accessing. It helps a system user identify processes using files or sockets.

Use the fuser command on the .git directory, to find all of the process ids which are accessing the directory.

fuser .git

Afterwards you can use the -k parameter to kill the processes, and then you should be able to delete the directory.

fuser -k .git

why os module has no way to delete non-empty directory?

The os module is not a wrapper for shell commands. os.rmdir is a wrapper for the C rmdir call, which is a low-level operation that cannot remove entire directory trees. Similarly, os.unlink is a wrapper for the C unlink function. (os.remove is also a wrapper for C unlink, rather than C remove, confusingly.)

If you want higher-level operations, some of those are available in shutil. For example, shutil.rmtree can remove an entire directory tree. This is not the default behavior for os.rmdir because that would be error-prone and more expensive.



Related Topics



Leave a reply



Submit