custom avatar removal

You just need to write a delete avatar function that gets the location of the file and deletes it using the php unlink function.

The following code can be used as an example of how it’s done. I use this to delete the old user avatars when a user uploads a new one. It is also used to replace default avatars that were once stored in a custom directory from an old plugin no longer being used.

function avatar_delete( $user_id ) {
            $old_avatars = get_user_meta( $user_id, 'author_avatar', true );
            $auth = get_the_author_meta( 'user_login', $user_id );
            $wp_content = WP_CONTENT_DIR;
            $filename = $wp_content . '/images/authors/' . $auth . '.jpg';
            if ( file_exists( $filename ) ) {

                unlink( $filename );
            }
            else {
                $upload_path = wp_upload_dir();

                if ( is_array( $old_avatars ) ) {
                    foreach ( $old_avatars as $old_avatar ) {
                        $old_avatar_path=str_replace( $upload_path[ 'baseurl' ], $upload_path[ 'basedir' ], $old_avatar );
                        @unlink( $old_avatar_path );
                    }
                }
            }

            delete_user_meta( $user_id, 'author_avatar' );
        }