@@ -739,7 +739,31 @@ func (s *Service) DeleteRestore(ctx context.Context, restore *models.Restore) er
739739 Msg ("Failed to remove service file (continuing anyway)" )
740740 }
741741
742- // 3. Destroy ZFS dataset (includes all data)
742+ // 2.5. Force kill any remaining PostgreSQL processes using the data directory
743+ dataDir := fmt .Sprintf ("/opt/branchd/%s/data" , restore .Name )
744+ s .logger .Info ().Str ("data_dir" , dataDir ).Msg ("Killing any remaining PostgreSQL processes" )
745+ killPostgresCmd := fmt .Sprintf (`
746+ pids=$(sudo lsof -t +D %s 2>/dev/null || true)
747+ if [ -n "$pids" ]; then
748+ echo "Killing processes: $pids"
749+ sudo kill -TERM $pids 2>/dev/null || true
750+ sleep 2
751+ # Check again and use SIGKILL if needed
752+ pids=$(sudo lsof -t +D %s 2>/dev/null || true)
753+ if [ -n "$pids" ]; then
754+ sudo kill -9 $pids 2>/dev/null || true
755+ fi
756+ fi
757+ ` , dataDir , dataDir )
758+ cmd = exec .CommandContext (ctx , "bash" , "-c" , killPostgresCmd )
759+ if output , err := cmd .CombinedOutput (); err != nil {
760+ s .logger .Warn ().
761+ Err (err ).
762+ Str ("output" , string (output )).
763+ Msg ("Failed to kill remaining processes (continuing anyway)" )
764+ }
765+
766+ // 3. Destroy ZFS dataset
743767 s .logger .Info ().Str ("zfs_dataset" , zfsDataset ).Msg ("Destroying ZFS dataset" )
744768 destroyCmd := fmt .Sprintf ("sudo zfs destroy -r %s" , zfsDataset )
745769 cmd = exec .CommandContext (ctx , "bash" , "-c" , destroyCmd )
@@ -754,10 +778,6 @@ func (s *Service) DeleteRestore(ctx context.Context, restore *models.Restore) er
754778 return fmt .Errorf ("failed to destroy ZFS dataset: %w" , err )
755779 }
756780
757- s .logger .Info ().
758- Str ("zfs_dataset" , zfsDataset ).
759- Msg ("ZFS dataset destroyed successfully" )
760-
761781 // 4. Delete restore record from SQLite
762782 if err := s .db .Delete (restore ).Error ; err != nil {
763783 s .logger .Error ().
0 commit comments