Revert "internal/postgres: remove documentation delete"

This reverts commit 101343b2dd9dbde2dcbdf934e05d636cba8b7a26.

Reason for revert: We still need to delete rows when an "all" row is
inserted to prevent panics in https://go.googlesource.com/pkgsite/+/master/internal/build_context.go#51.

Change-Id: Ic2dec11978774cde4bffeba040ef2e212385e994
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/305949
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go
index 09bbd3d..ea2a4bd 100644
--- a/internal/postgres/insert_module.go
+++ b/internal/postgres/insert_module.go
@@ -510,7 +510,20 @@
 	paths []string,
 	pathToUnitID map[string]int,
 	pathToDocs map[string][]*internal.Documentation) (err error) {
-	defer derrors.WrapStack(&err, "insertDocsCopy(%d paths)", len(paths))
+	defer derrors.WrapStack(&err, "insertDocs(%d paths)", len(paths))
+
+	// Remove old rows before inserting new ones, to get rid of obsolete rows.
+	// This is necessary because of the change to use all/all to represent documentation
+	// that is the same for all build contexts. It can be removed once all the DBs have
+	// been updated.
+	var unitIDs []int
+	for _, path := range paths {
+		unitIDs = append(unitIDs, pathToUnitID[path])
+	}
+	if _, err := db.Exec(ctx, `DELETE FROM documentation WHERE unit_id = ANY($1)`,
+		pq.Array(unitIDs)); err != nil {
+		return err
+	}
 
 	generateRows := func() chan database.RowItem {
 		ch := make(chan database.RowItem)