Search code examples
c#cloudinarycloudinarydotnet

CloudinaryDotNet not deleting folder in if it's had assets


I have the following code to delete folders in cloudinary using the CloudinaryDotNet nuget package

using System;
using System.Linq;
using CloudinaryDotNet;
using CloudinaryDotNet.Actions;

public class Program
{
    public static void Main()
    {
        var account = new Account("xxxxx", "xxxxx", "xxxxx");
        var instance = new Cloudinary(account);
        DeleteDirectory(instance, "TestFolder/AnotherFolder", true);
    }

    public static (string path, string name) GetPathAndName(string inputPath)
    {
        var path = string.Empty;
        var name = string.Empty;
        
        if (!string.IsNullOrWhiteSpace(inputPath))
        {
            var lastIndex = inputPath.LastIndexOf('/');
            if (lastIndex > -1)
            {
                path = inputPath.Substring(0, lastIndex);
                name = inputPath.Remove(0, lastIndex + 1);
                
                Console.WriteLine($"{path} {name}");
            }
            else
            {
                name = inputPath;
            }
        }

        return (path, name);
    }

    public static void DeleteFolder(Cloudinary instance, Folder folder, bool recursive)
    {
        instance.DeleteResourcesByPrefix(folder.Path);
        
        if (recursive)
        {
            var foldersResult = instance.SubFolders(folder.Path);
            if (foldersResult.TotalCount > 0)
            {
                foreach (var subFolder in foldersResult.Folders)
                {
                    DeleteFolder(instance, subFolder, recursive);
                }
            }
        }

        var result = instance.DeleteFolder(folder.Path);
        Console.WriteLine($"Deleted Folder: {result.StatusCode}");
    }

    public static Folder GetFolder(Cloudinary instance, string path)
    {
        var(pathStart, name) = GetPathAndName(path);
        var result = instance.SubFolders(pathStart);
        if (result.TotalCount > 0)
        {
            return result.Folders.FirstOrDefault(folder => folder.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
        }

        return null;
    }

    public static void DeleteDirectory(Cloudinary instance, string path, bool recursive)
    {
        var folder = GetFolder(instance, path);
        
        if (folder != null)
        {
            Console.WriteLine($"Folder Found: {folder.Name}");
            DeleteFolder(instance, folder, recursive);
        }
    }
}

This will work fine if the folder is empty, it will delete it and return an ok status.

The problem is if it has had images in the folder, it will delete the images, but then throw a bad request when it tries to delete the folder. If I then try to rerun it when the folder is empty, it will continue to throw the bad request.

Is there something else I need to do to delete the folder after it has had images in it?


Solution

  • The most likely cause for this is having backups enabled, and you're trying to delete a folder that is required in order to restore the backups in the future. If you get in touch with Cloudinary's support, we can apply a small change to your account to allow you to delete empty folders, regardless of their backup state.