1. Open a Command Prompt with administrative privileges.
  2. Run following commands, one at a time (change “Z:\Program Files” with folder you want to delete):

    takeown /F "Z:\Program Files" /A /R /D Y
    icacls "Z:\Program Files" /T /grant administrators:F
    rd /s /q "Z:\Program Files"

Note 1 – OS Language: takeown ... /D Y The input Y stands for ‘Yes’ and will be different depending on OS Language. Program Files folder may also be named differently.

Note 2 – Older versions of Windows: If icacls and rd are not supported then try use cacls and rmdir instead

Explanation and documentation:

The issue might be that you do not have the correct permissions in the discretionary access control lists (DACLs) for the folder and its content. DACLs identifies the trustees that are allowed or denied access to a securable object. So simply giving the ownership to the folder might not be enough, but you also needs to grant permission in the DACLs. You can grant permission using the icacls command as shown as example above. Access Control Lists and DACLs explained

takeown takeown documentation Administrator recovers access to a directories and it’s content that previously was denied, by making the administrators group the owner. /F [directory] specifies which directory, /A gives ownership to administrators group, /R performs it as recursive operation on directory, all files and sub-directories, /D suppresses confirmation prompts when user does not have “List Folder” permission with following Y option which take ownership of the directory. (Note: The Y option may be different depending on OS language).

icacls icacls documentation Grants the administrators group full access DAC permissions to directory. [directory] specify which directory, /T performs the operation on all specified files in directory and sub-directories, /grant grants specified user access rights with :F which gives full access. (Note: The group name administrators may be different depending on OS language)

rd rd documentation Deletes the directory with all its sub-directories and files. /s deletes the specified directory and its sub-directories including all files, /q specifies quiet mode so you get no prompt for confirmation, [directory] specify which directory to delete.