How to delete nonempty remote directories with GIO?

Hi all,

I have created a method for deleting directories in which g_file_measure_disk_usage() (see documentation link) is used at the first place to find out if the directory to be deleted is empty or not. If it is not empty, my program shows a query if the directory should be really deleted. If the user answers “yes”, my code steps through the directory content and removes contained files by calling g_file_delete() for each children item, and finally the directory to be removed is deleted also.

Problem: When being connected to a remote directory, for example via ssh, g_file_measure_disk_usage() returns the following error message:

Operation not supported for the current backend.

Now I am asking myself how to check for remote connections that the directory is empty or not. And furthermore, I wonder how to delete the directory via g_file_delete because in the documentation it is said that

If the file is a directory, it will only be deleted if it is empty.

So I have to check if it is empty before, and if not I have to step through the directory and delete the content before.

How is that possible when I get the mentioned error above, saying that g_file_measure_disk_usage() cannot be used for remote connections?

Obvious solution: always do that. Sounds like you’re already good to go, right? Just remove your call to g_file_measure_disk_usage()? Any problems with that?

1 Like

The best solution is to simply call g_file_delete and ask the user only in case you get G_IO_ERROR_NOT_EMPTY


Thank you both, @mcatanzaro and @oholy! Those are cool ideas, I’ll try to implement one of them in my project.