How to delete a permission denied file

I had a folder in Monterey Beta 12 that I could not delete, but this can happen any any macOS version. What happens from time to time is when trashing apps or doing updates a file will still be locked or linked to the original path or filename. This happened to me recently when I updated X11 on my Mac Mini M1. I tired the normal way of removing files but nothing would work. Here are some of what I had tried to no avail:

In this case the file I wanted to remove was in my .Trash folder: /Users/andy/.Trash/Previously\ Relocated\ Items

  1. sudo rm -rf /Users/andy/.Trash/Previously\ Relocated\ Items/X11
  2. sudo unlink /Users/andy/.Trash/Previously\ Relocated\ Items/X11; sudo rm -rf /Users/<user>/.Trash/Previously\ Relocated\ Items/X11

None of these would work, I even tried removing immutable thinking maybe it was an extended attribute thing. The only thing that would work for me was to boot it in recovery mode. The MacMini M1 is a bit different then prior systems as you will see below:

  1. On MacMini M1
    1. Shutdown
    2. Power on; holding the power button. Usually take 15sec or so but you will see the log come up then you can release.
    3. Click on Options
    4. Login
    5. On title bar:
      1. Find Security …
        1. Click on “unlock disk”
      1. Open Terminal
      2. cd /data/Users/<user>/.Trash
      3. rm -rf Previously\ Relocated\ Items/X11

Thats it, now you can restart and your docs will be gone, thanks for playing!