Programmatically delete local repository with LibGit2Sharp

I would like to delete the local repo folder that I cloned from remote repository using LibGit2Sharp. I read here here that I have to Dispose() the Repository before I can delete it.

LibGit2Sharp keeps hold on some files in the .git folder (mainly the packfiles for performance reasons). Calling Dispose() will release those handles and deallocate the non managed memory.

As such, it's indeed a strong recommendation to rely on the using statement (or, at the very least to Dispose() the Repository instance when you're done with it).

If you don't do this, those handles will eventually be released through finalizers when your AppDomain has unloaded, but you will have no real control regarding "when" that's going to happen.

Edit: Reading your code once again, I overlooked something. The recommended pattern is either

using (var repo = new LibGit2Sharp.Repository(path))
{
    // Do amazing stuff
}

or

var repo = new LibGit2Sharp.Repository(path);
// Do amazing stuff
repo.Dispose();

Indeed, the using statement will automatically issue a call to Dispose() once the code reach the end of the scope.

Access to the path 'c16566a7-202a-4c8a-84de-3e3caadd5af9' is denied.

Regarding this point, I think this has nothing to do with LibGit2Sharp.

Is the process (trying to delete the folder named after a guid) running under an identity granted with enough rights to do so?


For the benefit of anyone else having this problem:

I had the same problem, but I was still getting an UnauthorizedAccessException even though I was running as administrator, and I was disposing the repository object correctly. It turns out that some of the files in the .git folder are marked as ReadOnly, so I had to loop through each file and remove the ReadOnly attribute before deleting. I wrote a custom method to do this:

/// <summary>
/// Recursively deletes a directory as well as any subdirectories and files. If the files are read-only, they are flagged as normal and then deleted.
/// </summary>
/// <param name="directory">The name of the directory to remove.</param>
public static void DeleteReadOnlyDirectory(string directory)
{
    foreach (var subdirectory in Directory.EnumerateDirectories(directory)) 
    {
        DeleteReadOnlyDirectory(subdirectory);
    }
    foreach (var fileName in Directory.EnumerateFiles(directory))
    {
        var fileInfo = new FileInfo(fileName);
        fileInfo.Attributes = FileAttributes.Normal;
        fileInfo.Delete();
    }
    Directory.Delete(directory);
}