ForceOps 1.5.1

dotnet tool install --global ForceOps --version 1.5.1
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local ForceOps --version 1.5.1
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=ForceOps&version=1.5.1
                    
nuke :add-package ForceOps --version 1.5.1
                    

ForceOps

main build Nuget

Forcefully perform file operations by terminating processes that are using the file or directory.

Only supports windows, and not planned to support linux.

Uses LockChecker to find processes locking files, and will elevate itself if it the process is owned by another user.

Installation

It has a faster startup because it uses Native AOT:

scoop install https://gist.github.com/domsleee/f765105f512ec607ee0a6e3ee5debd6d/raw/forceops.json

To update:

scoop update forceops --force

Alternative - install with dotnet

dotnet tool install -g forceops

To update:

dotnet tool update -g forceops

Alternative - install from releases

Download the latest exe from releases.

Usage: As a CLI

Deleting when a process owned by the current user is using it

❯ forceops rm .\bin\
[14:55:33 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 1/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [100724 - myprogram.exe].
[14:55:34 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 2/10 in 50ms. ForceOps process is not elevated. Found 0 processes to try to kill: [].

Deleting when a process owned by another user is using it (e.g. a windows service)

❯ forceops rm .\bin\Debug\net6\myprogram.exe
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 1/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 2/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 3/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 4/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 5/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 6/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 7/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 8/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 9/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 10/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Exceeded retry count of 10. Failed. ForceOps process is not elevated.
[15:07:39 INF] Unable to perform operation as an unelevated process. Retrying as elevated and logging to "C:\Users\user\AppData\Local\Temp\tmp9C14.tmp".
[15:07:42 INF] Successfully deleted as admin

Usage: As a library

See the ForceOps.Lib package.

The easier way is to have an exe target using RelaunchHelpers.RunWithRelaunchAsElevated, which is shown in ForceOps.DeleteExample:

using ForceOps.Lib;

var fileOrDirectories = args.Select(arg => Path.Combine(Environment.CurrentDirectory, arg)).ToArray();
var forceOpsContext = new ForceOpsContext();
var fileAndDirectoryDeleter = new FileAndDirectoryDeleter(forceOpsContext);

RelaunchHelpers.RunWithRelaunchAsElevated(() =>
{
	foreach (var fileOrDirectory in fileOrDirectories)
	{
		fileAndDirectoryDeleter.DeleteFileOrDirectory(fileOrDirectory, true);
	}
}, () => args.ToList(), forceOpsContext);

Context

See Benchmarks on github pages.

Refer also to 10.4 Example: file deletion in Windows from "A Philosophy of Software Design", which explains why this is a windows specific problem. Linux does not prevent the user from deleting a file if it is being used, see unlink:

If the name was the last link to a file but any processes still have the file open, the file will remain in existence until the last file descriptor referring to it is closed.

Currently, only delete is supported.

Operations like move and copy can have similar issues if they are overriding files or the source file is in use. It would be reasonable to support these operations in a similar way.

For copying, consider using Microsoft.Build.CopyOnWrite.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
1.5.1 122 4/27/2025
1.5.0 135 5/31/2024
1.4.3 128 5/20/2024
1.4.2 110 5/20/2024
1.4.1 133 5/19/2024
1.4.0 121 5/19/2024
1.3.1 320 12/6/2023
1.3.0 216 12/3/2023
1.2.1 271 9/25/2023
1.2.0 265 9/7/2023
1.1.0 305 8/17/2023
1.0.4 195 7/11/2023
1.0.3 189 7/6/2023
1.0.2 175 7/5/2023
1.0.1 182 7/4/2023
1.0.0 180 7/3/2023
1.0.0-pre 302 6/30/2023