question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove-Item -Force cannot delete hidden file on Samba

See original GitHub issue

Steps 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:open
  • Created 3 years ago
  • Comments:28 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
jborean93commented, Mar 15, 2021

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 do

$a=[io.fileinfo]::new($uncpath)
$a.Delete()

# Or just
[IO.File]::Delete($uncpath)

It 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 to Normal. I assume this is because -Force is meant to force the deletion that fails when the ReadOnly flag is set.

image

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

  • Ignore this particular error if it occurs and just try the delete anyway, make removing the ReadOnly flag a best effort and use the actual deletion request what is reported on
  • Try to delete the file first and then unset the attribute if that failed with access denied

Personally 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).

0reactions
sba923commented, Sep 17, 2021

Note that I also get Remove-Item: The handle is invalid upon a Remove-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.:

-rwxrw-r-- 1 otheruser otheruser 21215 Jul 25  2010 foo

What’s next towards a solution to this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found