A few days ago I asked a question about deleting using WITH RECURSIVE
from PostgreSQL. There is:
That works fine: the intention, initially, was to delete parent folders recursively as long as the final child was deleted. The following image describes it better:
By deleting the file 5.jpg, all parent folders, in this situation, would be deleted as well.
But now I have to delete the parent folders only if they get empty, i.e. by losing its only child. I tried the following:
WITH RECURSIVE all_uploads (codigo, parent, ext, uploader) AS (
SELECT ut1.codigo, ut1.codigo_upload_temp_pai AS parent, ut1.codigo_extensao AS ext, ut1.codigo_usuario_inclusao AS uploader
FROM upload_temp ut1
WHERE ut1.codigo = 576
UNION ALL
SELECT ut2.codigo, ut2.codigo_upload_temp_pai AS parent, ut2.codigo_extensao AS ext, ut2.codigo_upload_temp_pai AS uploader
FROM upload_temp ut2
JOIN all_uploads au ON au.parent = ut2.codigo
WHERE (SELECT ut3.codigo FROM upload_temp ut3 WHERE ut3.codigo_upload_temp_pai = ut2.codigo LIMIT 1) IS NULL
AND ext IS NULL
AND uploader = 1535
)
DELETE FROM upload_temp WHERE codigo IN (SELECT codigo FROM all_uploads);
I thought the only way to check if a folder is empty is to perform a sub-select considering the self relationship. If SELECT ut3.codigo FROM upload_temp ut3 WHERE ut3.codigo_upload_temp_pai = ut2.codigo LIMIT 1) IS NULL
returns true, so the folder is empty. And by using the self referencing feature (same DB table for folders and files), I know it's a folder by checking codigo_extensao
field (only files have extensions).
Well, it's not working, it removes only my 5.jpg. Any hint? Thanks in advance!