Files
supabase/apps/studio/components/interfaces/Integrations/Warehouse/WarehouseDisableCard.tsx
Danny White 279434fb70 feat(studio): add copied data deletion to Warehouse disable dialog (#51906)
## Problem

Disabling Warehouse leaves copied data in storage. Users need an
explicit option to remove it without deleting files manually.

Closes [PIPE-1128](https://linear.app/supabase/issue/PIPE-1128), using
the deletion support from
[PIPE-1098](https://linear.app/supabase/issue/PIPE-1098).

## Solution

Adds “Also delete copied Warehouse data” to the disable dialog. It stays
unchecked by default. Source tables are preserved.

| Before | After |
| --- | --- |
| <img width="826" height="504" alt="CleanShot 2026-10-19 at 16 17
12@2x"
src="https://github.com/user-attachments/assets/defc99b9-4ec0-4188-bfa6-53f489ef2be5"
/> | <img width="830" height="614" alt="CleanShot 2026-10-19 at 16 36
40@2x"
src="https://github.com/user-attachments/assets/80299728-a790-4d80-a2d9-16750977a679"
/> |

Handles errors differently depending on what it relates to:

| General Errors | Data Deletion Only |
| --- | --- |
| <img width="840" height="864" alt="CleanShot 2026-10-20 at 13 22
05@2x"
src="https://github.com/user-attachments/assets/7b2b37c3-ace6-4fe0-a1b7-106a97eaab0a"
/> | <img width="854" height="876" alt="CleanShot 2026-10-20 at 13 19
13@2x"
src="https://github.com/user-attachments/assets/81459525-bb9f-4d2c-891d-b7f8f646098a"
/> |
| _Above the fold_ | _Below the fold, within the relevant section_ |

## Review instructions

1. Open **Integrations → Warehouse → Overview** in a project with
Warehouse enabled, then click **Disable Warehouse**.
2. Check that **Also delete copied Warehouse data** is unchecked and has
a permanent deletion warning. Select it, cancel, and reopen. It should
be unchecked again.
3. On a disposable project, confirm with the option unchecked. Copied
data should remain. With the option checked, the managed DuckLake
catalogue and Storage bucket should be removed. Source tables should
remain in both cases.
4. Confirm Studio shows cleanup progress, then returns to setup. Failed
requests should keep the dialog open; failed background cleanup should
offer **Retry**.
2026-10-20 14:40:05 +11:00

197 lines
7.0 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useParams } from 'common'
import { useRef, useState } from 'react'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
AlertDialog,
AlertDialogAction,
AlertDialogBody,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
Button,
Card,
CardContent,
Checkbox,
Form,
FormControl,
FormField,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { FormLayout } from 'ui-patterns/form/Layout/FormLayout'
import {
PageSection,
PageSectionContent,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { z } from 'zod'
import { useWarehouseReplicatedTargets } from './useWarehouseReplicatedTargets'
import { type WarehouseSetupTarget } from './Warehouse.utils'
import { AlertError } from '@/components/ui/AlertError'
import { useWarehouseDisableMutation } from '@/data/warehouse/warehouse-disable-mutation'
import { useTrack } from '@/lib/telemetry/track'
const FormSchema = z.object({ shouldDeleteData: z.boolean() })
type FormValues = z.infer<typeof FormSchema>
const defaultValues: FormValues = { shouldDeleteData: false }
export const WarehouseDisableCard = () => {
const { ref: projectRef } = useParams()
const form = useForm<FormValues>({ resolver: zodResolver(FormSchema), defaultValues })
const track = useTrack()
const [isConfirming, setIsConfirming] = useState(false)
const replicatedTargets = useWarehouseReplicatedTargets({ projectRef })
// Snapshotted when the user confirms: the mutation's own onSuccess awaits the post-disable
// refetches, after which nothing reads as replicated.
const targetsAtConfirm = useRef<WarehouseSetupTarget[] | undefined>(undefined)
const disableMutation = useWarehouseDisableMutation({
onSuccess: async () => {
// Tracked from the mutation's onSuccess, not mutateAsync's: the status refetch swaps this
// card out of the panel, and mutate-level callbacks are skipped once it has unmounted.
const targets = targetsAtConfirm.current
track('warehouse_disabled', {
...(targets !== undefined && {
schemaTargetCount: targets.filter((target) => target.type === 'schema').length,
tableTargetCount: targets.filter((target) => target.type === 'table').length,
}),
})
setIsConfirming(false)
toast.success('Warehouse disable requested')
},
onError: () => {},
})
const handleDisable = ({ shouldDeleteData }: FormValues) => {
if (!projectRef) return undefined
targetsAtConfirm.current = replicatedTargets
// Returned so the dialog stays open on failure and closes once the disable succeeds.
return disableMutation.mutateAsync({ projectRef, shouldDeleteData })
}
const disableError = disableMutation.error
const isLegacyStorageError =
disableError?.message === 'Legacy Warehouse data must be retained when disabling Warehouse.'
return (
<PageSection className="pt-5!">
<PageSectionMeta>
<PageSectionSummary>
<PageSectionTitle>Disable</PageSectionTitle>
</PageSectionSummary>
</PageSectionMeta>
<PageSectionContent>
<Card>
<CardContent>
<FormLayout
layout="flex-row-reverse"
label="Disable Warehouse for this project"
description="Stops replication and removes Warehouse-managed resources."
>
<Button
variant="danger"
onClick={() => {
form.reset()
disableMutation.reset()
setIsConfirming(true)
}}
>
Disable Warehouse
</Button>
</FormLayout>
</CardContent>
</Card>
</PageSectionContent>
<AlertDialog open={isConfirming} onOpenChange={setIsConfirming}>
<AlertDialogContent size="small">
<AlertDialogHeader>
<AlertDialogTitle>Disable Warehouse</AlertDialogTitle>
</AlertDialogHeader>
<AlertDialogBody>
<AlertDialogDescription asChild>
<div className="space-y-2">
<p>
Stops replication and disconnects the Warehouse endpoint. Removes its pipeline,
publication, catalog access, and foreign tables.
</p>
</div>
</AlertDialogDescription>
{disableError && !isLegacyStorageError && (
<div className="px-5 pb-4">
<AlertError
subject="Unable to disable Warehouse"
showErrorPrefix={false}
showInstructions={false}
hideContactSupport
error={disableError}
/>
</div>
)}
<div className="border-t px-5 py-4">
<Form {...form}>
<FormField
control={form.control}
name="shouldDeleteData"
render={({ field }) => (
<FormItemLayout
layout="flex"
label="Also delete copied Warehouse data"
description="Deletes the copied data and its storage permanently. Source tables are preserved."
>
<FormControl>
<Checkbox
name={field.name}
ref={field.ref}
onBlur={field.onBlur}
checked={field.value}
onCheckedChange={(checked) => field.onChange(checked === true)}
disabled={disableMutation.isPending}
/>
</FormControl>
</FormItemLayout>
)}
/>
</Form>
</div>
{isLegacyStorageError && (
<div className="px-5 pb-4">
<AlertError
subject="Copied data cannot be deleted"
showErrorPrefix={false}
showInstructions={false}
hideContactSupport
error={{
message: 'Turn this option off to disable Warehouse and keep the copied data.',
}}
/>
</div>
)}
</AlertDialogBody>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
variant="danger"
loading={disableMutation.isPending}
disabled={!projectRef}
onClick={() => {
return form.handleSubmit(handleDisable)()
}}
>
Disable Warehouse
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</PageSection>
)
}