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
[FEATURE REQUEST] Provide file.absent
with removedirs
option
#61330
Comments
@jgraichen May I direct you to file.remove which already performs the operation you desire, see Also https://docs.saltproject.io/en/latest/ref/states/all/salt.states.file.html#salt.states.file.absent for file.absent states
Hence file.absent deletes a provided directory path, just tried it and it works. Closing this issue since functionality already exists, but please re-open if need to bring some other information to attention. |
This is more about deleting empty parent directories, not nested content. Image the following state, which write a configuration file, or removes it, if the pillar value is not set:
This is a basic scenario, but with loops, and deeper directories, a For example, in the above example, when |
@jgraichen I actually tried with empty sub-directories and files and they were all deleted, including the specified parent directory. Please give a specific test case where the command is not working, and file it as a bug report. Note file.absent will only remove a file if the specified input is a file, if the specified input is a directory then it removes the directory recursively. |
Please note that this is not about deleting a directory and its content! It's the contrary, deleting only all empty parent directories. Please see the example state code and the SLS example. Please also see the difference between What I am looking for is not a bug fix, but a feature, for cases such as [1] in nested scenarios, an inverse to See this example:
This will keep the empty directory See this example too:
This is an example application that requires a configuration file for each loaded module, but also does not start if any directory or config files for non-loaded module is present. Therefore,
But be aware, the application just wrote a file with leading dot (e.g. All we did want is Assume we have two entirely different states (or even some Debian package), that need to put files in the same location, e.g. a system path:
These states will just delete each other config files. This would be much easier if both could just have Please let me repeat, this is NOT about deleting a single directory and its content. It is the contrary! |
re-opening as Feature Request |
@jgraichen A PR with tests would help make the Feature Request proceed quicker, not saying it would be accepted, just working code always helps, otherwise it could be quite a while before it is implemented if the Feature Request is accepted. |
Is your feature request related to a problem? Please describe.
file.managed
does supportmakedirs: True
for creating the directories need to write a file. When that file is no longer needed,file.absent
can remove that file, but all directories are kept.Describe the solution you'd like
I'd like to have a
removedirs: True
option onfile.absent
that works like Python'sos.removedirs()
function and removes all empty parent directories after removing the file.This also looks very symmetrical to
file.managed
withmakedirs: True
.Describe alternatives you've considered
The most common solution mentioned is using
file.absent
on the parent directory withunless:
and some glob check, but that has many edge cases. Just running a/path/to/*
glob often will not detect.hidden
files, and it only works for a single directory level. A custom state could be added, but needs to be maintained separately:(This one does not change the comment, but extends the state diff with deleted parent directories.)
The text was updated successfully, but these errors were encountered: