I want to use rimraf command in powershell script to delete all files and sub-folders in certain directory (named "Results"). I am testing rimraf on virtual drive ("B") and need to use network path, so my command looks like rimraf '\\PC name\B\Results\*'
, but it does nothing and I can´t figure out the correct syntax. I´ve tried many different variations of the path, but none was working.

- 13
- 3
1 Answers
The rimraf
Node.js module and CLI normally does perform its own globbing on Windows, so - given that argument '\\PC name\B\Results\*'
, as a verbatim string, is passed unmodified to rimraf
, the implication is that rimraf
somehow doesn't support globbing on your .vhd
drive.
Note: The -G
command-line option disables globbing, but it is enabled by default.
The workaround is to let PowerShell do the globbing.
The following should work, but doesn't as of rimraf
v3.0.2 / Node.js v14.15.12:
# !! Currently does NOT work - even though it should.
# CAVEAT: INSTANTLY DELETES ALL CONTENT IN THE TARGET DIR.
rimraf -G (Get-Item '\\PC name\B\Results\*' -Force).FullName
Seemingly, passing multiple paths (the Get-Item
call results in an array of file/directory names whose elements are passed as individual arguments) is currently broken.
Therefore, use the following, which, however, is slow, because rimraf
is invoked separately for each file and directory:
# Should work, but is slow.
# CAVEAT: INSTANTLY DELETES ALL CONTENT IN THE TARGET DIR.
(Get-Item '\\PC name\B\Results\*' -Force).FullName | % { rimraf -G $_ }
Of course, you can also use PowerShell's own Remove-Item
cmdlet:
# CAVEAT: If you remove -WhatIf, instantly deletes all content in the target dir.
Remove-Item '\\PC name\B\Results\*' -Recurse -Force -WhatIf
Note that, irrespective of the tool used for deletion, deleting directory subtrees on Windows versions before Windows 10 20H2 can fail intermittently, due to the previously asynchronous behavior of file deletion - see this answer.

- 382,024
- 64
- 607
- 775
-
1Thank you very much for all the information and your solution! – JakubDlabka Jan 15 '21 at 13:59
-
I'm glad to hear it was helpful, @JakubDlabka; my pleasure. – mklement0 Jan 15 '21 at 14:13