This simple Blender Addon allows you to remove all unused material slots from a mesh with just one click.
Just download the release ZIP file.
Then open Blender and go to Edit -> Preferences -> Addons. Click on "Install..." and select the downloaded ZIP file. Finally enable it using the checkbox.
When successfully installed, just select the mesh you want to clean up. Then go to Object -> Remove Unused Material Slots.