remotely log out of (graphical) gnome session

Solution 1:

After logging in with ssh, run:

env DISPLAY=:0.0 gnome-session-quit --logout

This will force a logout on the remote machine just as if you had logged out from the menu (but without prompting). You may need to run gnome-session-quit with --force-logout if there's an application with, for example, unsaved work, that would otherwise prevent a clean logout.

If you use a very old version (<2011) of GNOME, then you need to

env DISPLAY=:0.0 gnome-session-save --logout

... because gnome-session-save was renamed to gnome-session-quit in 2011.

Source