How to remove SID (session ID) from URL in Magento 2

Go to the Store > Configuration > General > Web > Session Validation Settings > Use SID on Storefront and set its value to No

example

Here is the good answer about the SID:

The SID is a "session ID". Magento uses this to track a user's activity within the same Magento installation. Normally, Magento powers one website and one store from one installation (database).

Magento could power multiple websites with multiple stores from one installation though. The SID allows users to stay logged in while navigating across these websites/stores.

I think if you have the function enabled, the SID is sent when accessing catalog URLs so Magento can update the session with the user's location/state for the current website/store.

If you're not running a multi-website or multi-store environment, it's safe to disable the SID on the frontend.


Those of you who already set Use SID on Storefront to No (as Siarhey Uchukhlebau pointed out in his answer) but still can't get rid of &sid=... in the URL, check out following links.

It is a Magento Bug, which will be fixed in version 2.3
Related ticket: https://github.com/magento/magento2/issues/9453

Fortunately, there is a Inchoo Tutorial on how to remove it via .htaccess file as workaround
http://inchoo.net/dev-talk/remove-sid-from-magento-urls/


Isn't ?SID automatically added by PHP if PHP is configured to not store sessions on the server or the browser doesn't accept cookies?

I believe Magento doesn't do this by default. There is a setting for it, but I'm assuming that the OP didn't change this, otherwise he would have figured this out by himself.