Remove-Item -Force cannot delete hidden file on Samba
See original GitHub issueSteps to reproduce
Attempting to delete a hidden file that is stored on a Samba file share (Samba 4.11.6-Ubuntu on Ubuntu 20.04.2 LTS) fails, whereas CMD can delete the file
PowerShell:
PS❯ gci -force '\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx'
Directory: S:\MM\catalogs
Mode LastWriteTime Length Name
---- ------------- ------ ----
---h- 2016-01-26 09:46 12.67KB .NET assembly dependency (2020_09_20 08_01_14 UTC).docx
PS❯ remove-item '\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx'
Remove-Item: You do not have sufficient access rights to perform this operation or the item is hidden, system, or read only.
PS❯ remove-item -force '\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx'
Remove-Item: The handle is invalid. : '\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx'
CMD:
C:\Users\<REDACTED>>dir /ah "\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx
Volume in drive \\pnjnas\public is Public
Volume Serial Number is 4664-C5A0
Directory of \\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive
2016-01-26 09:46 12,979 .NET assembly dependency (2020_09_20 08_01_14 UTC).docx
1 File(s) 12,979 bytes
0 Dir(s) 1,390,021,279,744 bytes free
C:\Users\<REDACTED>>del /ah "\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx
C:\Users\<REDACTED>>dir /ah "\\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive\.NET assembly dependency (2020_09_20 08_01_14 UTC).docx
Volume in drive \\pnjnas\public is Public
Volume Serial Number is 4664-C5A0
Directory of \\pnjnas\public\FileHistory\LATITUDE7400\Data\C\Users\sto\OneDrive
File Not Found
Expected behavior
Remove-Item -Force
should delete the file
Actual behavior
Remove-Item -Force
fails with an “invalid handle” error.
Environment data
> $psversiontable
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:28 (11 by maintainers)
Top Results From Across the Web
linux - How to delete .fuse_hidden* files?
I have small Linux server (Debian Squeeze) which runs a Samba server which is configured to share some folders with some windows machines....
Read more >How can I delete a hidden folder?
I want to delete many configuration folder in my home user folder but I can't figure out how to delete them. How can...
Read more >powershell script to delete hidden files
Solution: There are three ways to go about this:This will remove just hidden files:Get-ChildItem "path" -Directory | Get-ChildItem -hidden ...
Read more >Force remove a hidden file in windows - Computer How To
To forcibly remove a hidden file the best thing to use is the windows command line. To access the command line interface click...
Read more >[SOLVED] deleting hidden files and folders help
I need to delete one of the folders and it says that I can't because the directory is not empty. Well, it is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue here is the
chmod 544
is setting permissions for the Samba user to just read and execute without any write access. For a delete operation this is fine as you just need to make sure you open the file with that request right. When you doIt will open the file with the Delete access mask which should still be fine.
When you do
Remove-Item ...
it is actually opening the file with read and write access to the attributes and setting the attributes toNormal
. I assume this is because-Force
is meant to force the deletion that fails when theReadOnly
flag is set.Because the samba user does not have write access it is failing to open the handle to that file and thus fails to delete the attributes. The fact that it’s a compound request is probably the reason why the error back is invalid handle rather than access denied because the last request failed with that.
For fixing this problem I see 2 ways this could be done
ReadOnly
flag a best effort and use the actual deletion request what is reported onPersonally I think the first option is the way to go, it doesn’t add/remove any extra steps than what is currently done and if the user is truly unable to delete the file then the error back from the deletion request will still reflect that. The 2nd option might be quicker in normal cases but it could add an extra step if it does need to remove the attribute (and it’s not needed).
Note that I also get
Remove-Item: The handle is invalid
upon aRemove-Item -LiteralPath foo -Verbose -Force
, for a non-hidden file that is just not writable for the user used for the connection to the Samba share, i.e.:What’s next towards a solution to this issue?