Mac OS X - Unable to sudo delete a file or folder

If you can't edit, modify, or delete a file or folder as root (or via sudo) on your Mac, you've just run into Apple's "Rootless System Integrity Protection" on OS X El Capitan 10.11 and newer.

I ran into this problem trying to modify a local instance of PHP running on my Mac, which had my extensions directory installed in the /usr/lib/php/extensions location.

The folders Apple protects include the following:

/System
/sbin
/usr (with an exception of /usr/local)

Step 1: Reboot your Mac in Recovery mode

Reboot your Mac while holding down both the Command and R keys at the same time until the Recovery Screen shows up. Once the Recovery Screen is displayed, launch the Terminal app from the Utilities menu at the top of the screen.

Step 2: Disable rootless

Typing the command below into the terminal will disable rootless and restart your Mac:

csrutil disable; reboot  

That's it...

You can now modify files and folders in the protected directories. Want to turn protection back on?

How to re-enable rootless

Repeat Step 1, and then type the following in the Terminal to enable rootless and reboot your Mac:

csrutil enable; reboot