WordPress: New user role which is ONLY allowed to manage media

The add_role function should give you what you’re looking for. Here’s an example of using it to add a new role that allows the user to upload and delete media:

function wpse413985_add_role(){

/* Check if the role already exists, since we 
don't need to add it again every time the site loads. */
  if ( !get_role( 'wpse413985_media_manager' ) ){
    
     add_role( 
      'wpse413985_media_manager', 
      __( 'Media Manager', 'wpse413985-textdomain' ),
      array( 
        'upload_files' => true,
        'delete_posts' => true
      ));

  }
}

add_action( 'init', "wpse413985_add_role" );

A user with the ‘media manager’ role in this example will see a link to the media library but none of the other admin screens. The delete posts capability is necessary to delete pictures because media files count as ‘posts’ in wordpress. The user won’t be able to delete regular posts because they can’t visit that part of the admin.

This function would go in a theme’s functions.php. However, you could also put it in a plugin. In that case, instead of hooking to the init action, you could use register_activation_hook. That would eliminate the need for the conditional.