Empty Media Recycle Bin

Hi all,
I have umbraco 8.1.5 website and I want to delete a folder which contains another subfolders from Media Recycle bin. My structure is:

When I right click on Patient Images and click delete I got the following error:

The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "FK_umbracoNode_umbracoNode_id". The conflict occurred in database "VMDent_Live", table "dbo.umbracoNode", column 'parentId'. The statement has been terminated.

I understand why I am getting the error - “Patent Images” is parent of the subnodes, but I have a lot of subnodes and deleting them manually is not an option.
Is there a way to delete the parent folder with all of its subnodes.


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/101599-empty-media-recycle-bin

Updated code tested with Umbraco 13 if someone needs it.

-- Delete property data
DELETE FROM umbracoPropertyData 
WHERE versionId IN (
    SELECT id FROM umbracoContentVersion 
    WHERE nodeId IN (SELECT id FROM @NodesToDelete)
);

-- Delete document versions
DELETE FROM umbracoDocumentVersion 
WHERE id IN (
    SELECT id FROM umbracoContentVersion 
    WHERE nodeId IN (SELECT id FROM @NodesToDelete)
);

-- Delete content versions
DELETE FROM umbracoContentVersion 
WHERE nodeId IN (SELECT id FROM @NodesToDelete);

-- Delete documents
DELETE FROM umbracoDocument 
WHERE nodeId IN (SELECT id FROM @NodesToDelete);

-- Delete content
DELETE FROM umbracoContent 
WHERE nodeId IN (SELECT id FROM @NodesToDelete);

-- Delete redirects
DELETE FROM umbracoRedirectUrl 
WHERE contentKey IN (SELECT uniqueId FROM umbracoNode WHERE id IN (SELECT id FROM @NodesToDelete));

-- Delete relations (both parent & child)
DELETE FROM umbracoRelation 
WHERE parentId IN (SELECT id FROM @NodesToDelete) 
   OR childId IN (SELECT id FROM @NodesToDelete);

-- Finally, delete the nodes
DELETE FROM umbracoNode WHERE id IN (SELECT id FROM @NodesToDelete);
1 Like