Cleaning up + save workspace when removing reference

- Clean up
- save workspace when removing reference

Fixes #1860

review: @devoncarew
cc: @gaurave