The best way should be to switch the 5V and leave everything else connected. Otherwise you would have to make sure that at least the ground line is connected before the data lines. That's what the standard plugs do when plugging in – the power contacts are longer than the data contacts and connect first.
An important part to know is that the USB upstream (host or hub) recognize a connected device by a pull-up resistor connected by the device to one of the data lines. The device recognizes an active upstream by the presence of the 5V supply.
Now let's look at what happens when switching. There are two cases.
For the bus powered device, it will lose power when the 5V is switched off. That causes the pull-up to drop. No pull-up → upstream sees a disconnect and the host's USB stack will handle that properly. The upstream has weak pull-downs on the data lines so once it stops driving the data lines because it believes the device to be disconnected there will be no voltage above ground on any of the lines still connected. No need to disconnect the data lines therefore.
A self powered device on the other hand will keep running when the bus 5V disappears. That can happen without any switching shenanigans on any regular computer, namely when it powers off or is sent into suspend. The device will recognize a disconnect of the upstream. In that case, the USB standard requires the device to switch off the pull-up on the data line to prevent a steady leakage current from flowing into a powered down upstream. In our case of the 5V switch, the disconnect of the pull-up will be recognized by upstream as a device disconnect. Both upstream and device are still powered but believe they are disconnected from each other.
Reconnecting the 5V will in both cases be recognized as a plug in event when the pull-up appears on the relevant data line.