Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DirAccess method to remove a non-empty directory #11598

Open
Elip100 opened this issue Jan 17, 2025 · 5 comments
Open

Add a DirAccess method to remove a non-empty directory #11598

Elip100 opened this issue Jan 17, 2025 · 5 comments

Comments

@Elip100
Copy link

Elip100 commented Jan 17, 2025

Describe the project you are working on

A 2D physics based rage game

Describe the problem or limitation you are having in your project

Deleting a directory of save data is annoying to do

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add ability to delete an entire (non-empty) directory and all the files in it

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

DirAccess.idk_what_the_function_would_be_called("user://save")

If this enhancement will not be used often, can it be worked around with a few lines of script?

Currently I am manually updating a list of all the files to be deleted and using a for loop to loop through and delete them all. This is very inconvenient and annoying.

Is there a reason why this should be core and not an add-on in the asset library?

It is super easy to implement and will probably only take around 5-10 lines of code

@sockeye-d
Copy link

If you want to recursively delete a directory you could probably do this:

func remove_recursive(directory: String) -> void:
    for dir_name in DirAccess.get_directories_at(directory):
        remove_recursive(directory.path_join(dir_name))
    for file_name in DirAccess.get_files_at(directory):
        DirAccess.remove_absolute(directory.path_join(file_name))

    DirAccess.remove_absolute(directory)

@Calinou Calinou changed the title Ability to delete a non-empty directory Add a DirAccess method to remove a non-empty directory Jan 18, 2025
@Elip100
Copy link
Author

Elip100 commented Jan 18, 2025

If you want to recursively delete a directory you could probably do this:

func remove_recursive(directory: String) -> void:
for dir_name in DirAccess.get_directories_at(directory):
remove_recursive(directory.path_join(dir_name))
for file_name in DirAccess.get_files_at(directory):
DirAccess.remove_absolute(directory.path_join(file_name))

DirAccess.remove_absolute(directory)

Yes but it would still be better to just have one method to do it

@sockeye-d
Copy link

Technically you could do OS.execute("rm", ["-rf", path]) and that's on one line

@Elip100
Copy link
Author

Elip100 commented Jan 19, 2025

Technically you could do OS.execute("rm", ["-rf", path]) and that's on one line

That only works on Linux and Mac, not Windows.

@sockeye-d
Copy link

That only works on Linux and Mac, not Windows.

I never said anything about it being a good solution. Personally I don't think such a method is necessary given that it's trivial to write a custom method and put it in a utils class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants