11

I am having one protobuf message -

message Sample{
    string field1 = 1;
    string field2 = 2;
    string field3 = 3;
}

These messages are stored in datastore in binary format. So if I want to remove any of the defined field in the above message will it cause any issue in deserialization of the message from datastore?

Antonio
  • 19,451
  • 13
  • 99
  • 197
Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30

2 Answers2

17

No. Removing fields is fine, although you might want to mark it reserved so that nobody reuses it in an incompatible way. New code with old data (with the field) will silently ignore it; old code with new data will just load without the field populated, since everything in proto3 is implicitly optional. This was more of a problem in proto2, when required was a thing. Another option is to leave the field but mark it with [deprecated = true] - it'll still exist and be populated, but some tools will mark the member with the platform-specific obsolete markers for that language/framework.

Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for help. This documentation says the same - https://developers.google.com/protocol-buffers/docs/proto3#updating – Rahul Vedpathak Jul 09 '20 at 05:08
  • @RahulVedpathak Please accept the answer if it solved your problem. – Antonio Dec 21 '21 at 06:35
  • 1
    Could you remove the field, and then reuse the field number at a later time, if you could guarantee that there is no code anywhere that is still assuming the field number refers to the old field? – James Wierzba Apr 04 '22 at 23:05
  • 1
    @JamesWierzba you'd also need to guarantee that no persisted data (files, cache, databases, etc) uses the old data. Honestly, it just isn't worth the risk. Use a new number. – Marc Gravell Apr 05 '22 at 17:33
  • added a link to reserved fields doc https://developers.google.com/protocol-buffers/docs/proto3#reserved – Alok Kumar Singh Jul 25 '22 at 06:16
2

Adding and removing fields will not cause safety issues as mentioned in the answer from Marc. The only safety you should care about it is to mark the field as reserved. This will ensure that no one uses the same field number accidentally in future

kozmo
  • 4,024
  • 3
  • 30
  • 48