Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Support emptying bucket (#20335)

Joshen Limd609d0d594ee
diff --git a/apps/studio/components/layouts/StorageLayout/BucketRow.tsx b/apps/studio/components/layouts/StorageLayout/BucketRow.tsx
index 94b8ccc3f99..fb54059a4d3 100644
--- a/apps/studio/components/layouts/StorageLayout/BucketRow.tsx
+++ b/apps/studio/components/layouts/StorageLayout/BucketRow.tsx
@@ -14,6 +14,7 @@ import {
IconEdit2,
IconLoader,
IconTrash,
+ IconXCircle,
} from 'ui'
import { Bucket } from 'data/storage/buckets-query'
@@ -23,14 +24,16 @@ export interface BucketRowProps {
bucket: Bucket
projectRef?: string
isSelected: boolean
- onSelectDeleteBucket: (bucket: Bucket) => void
- onSelectEditBucket: (bucket: Bucket) => void
+ onSelectEmptyBucket: () => void
+ onSelectDeleteBucket: () => void
+ onSelectEditBucket: () => void
}
const BucketRow = ({
bucket,
projectRef = '',
isSelected = false,
+ onSelectEmptyBucket = noop,
onSelectDeleteBucket = noop,
onSelectEditBucket = noop,
}: BucketRowProps) => {
@@ -82,7 +85,7 @@ const BucketRow = ({
<DropdownMenuItem
key="toggle-private"
className="space-x-2"
- onClick={() => onSelectEditBucket(bucket)}
+ onClick={() => onSelectEditBucket()}
>
<IconEdit2 size="tiny" />
<p>Edit bucket</p>
@@ -91,7 +94,15 @@ const BucketRow = ({
<DropdownMenuItem
key="delete-bucket"
className="space-x-2"
- onClick={() => onSelectDeleteBucket(bucket)}
+ onClick={() => onSelectEmptyBucket()}
+ >
+ <IconXCircle size="tiny" />
+ <p>Empty bucket</p>
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ key="delete-bucket"
+ className="space-x-2"
+ onClick={() => onSelectDeleteBucket()}
>
<IconTrash size="tiny" />
<p>Delete bucket</p>
diff --git a/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx b/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx
index bf51e179f43..5b3920c8ffa 100644
--- a/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx
+++ b/apps/studio/components/layouts/StorageLayout/StorageMenu.tsx
@@ -23,6 +23,7 @@ import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import { useBucketsQuery } from 'data/storage/buckets-query'
import { useCheckPermissions, useSelectedProject } from 'hooks'
import BucketRow from './BucketRow'
+import { EmptyBucketModal } from 'components/to-be-cleaned/Storage/EmptyBucketModal'
const StorageMenu = () => {
const router = useRouter()
@@ -32,6 +33,7 @@ const StorageMenu = () => {
const [showCreateBucketModal, setShowCreateBucketModal] = useState(false)
const [selectedBucketToEdit, setSelectedBucketToEdit] = useState<StorageBucket>()
+ const [selectedBucketToEmpty, setSelectedBucketToEmpty] = useState<StorageBucket>()
const [selectedBucketToDelete, setSelectedBucketToDelete] = useState<StorageBucket>()
const canCreateBuckets = useCheckPermissions(PermissionAction.STORAGE_ADMIN_WRITE, '*')
@@ -132,6 +134,7 @@ const StorageMenu = () => {
bucket={bucket}
projectRef={ref}
isSelected={isSelected}
+ onSelectEmptyBucket={() => setSelectedBucketToEmpty(bucket)}
onSelectDeleteBucket={() => setSelectedBucketToDelete(bucket)}
onSelectEditBucket={() => setSelectedBucketToEdit(bucket)}
/>
@@ -163,6 +166,12 @@ const StorageMenu = () => {
onClose={() => setSelectedBucketToEdit(undefined)}
/>
+ <EmptyBucketModal
+ visible={selectedBucketToEmpty !== undefined}
+ bucket={selectedBucketToEmpty}
+ onClose={() => setSelectedBucketToEmpty(undefined)}
+ />
+
<DeleteBucketModal
visible={selectedBucketToDelete !== undefined}
bucket={selectedBucketToDelete}
diff --git a/apps/studio/components/to-be-cleaned/Storage/EmptyBucketModal.tsx b/apps/studio/components/to-be-cleaned/Storage/EmptyBucketModal.tsx
new file mode 100644
index 00000000000..4a4544baf73
--- /dev/null
+++ b/apps/studio/components/to-be-cleaned/Storage/EmptyBucketModal.tsx
@@ -0,0 +1,63 @@
+import { useParams } from 'common'
+import toast from 'react-hot-toast'
+
+import ConfirmationModal from 'components/ui/ConfirmationModal'
+import { useBucketEmptyMutation } from 'data/storage/bucket-empty-mutation'
+import { Bucket } from 'data/storage/buckets-query'
+import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'
+import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ IconAlertTriangle,
+ Modal,
+} from 'ui'
+
+export interface EmptyBucketModalProps {
+ visible: boolean
+ bucket?: Bucket
+ onClose: () => void
+}
+
+export const EmptyBucketModal = ({ visible = false, bucket, onClose }: EmptyBucketModalProps) => {
+ const { ref: projectRef } = useParams()
+ const { fetchFolderContents } = useStorageStore()
+
+ const { mutate: emptyBucket, isLoading } = useBucketEmptyMutation({
+ onSuccess: async () => {
+ if (bucket === undefined) return
+ await fetchFolderContents(bucket.id, bucket.name, -1)
+ toast.success(`Successfully deleted bucket ${bucket!.name}`)
+ onClose()
+ },
+ })
+
+ const onEmptyBucket = async () => {
+ if (!projectRef) return console.error('Project ref is required')
+ if (!bucket) return console.error('No bucket is selected')
+ emptyBucket({ projectRef, id: bucket.id })
+ }
+
+ return (
+ <ConfirmationModal
+ danger
+ size="small"
+ visible={visible}
+ onSelectCancel={() => onClose()}
+ onSelectConfirm={onEmptyBucket}
+ header={`Confirm to delete all contents from ${bucket?.name}`}
+ buttonLabel="Empty bucket"
+ >
+ <Modal.Content className="py-4 space-y-2">
+ <Alert_Shadcn_ variant="warning">
+ <IconAlertTriangle strokeWidth={2} />
+ <AlertTitle_Shadcn_>This action cannot be undone</AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ The contents of your bucket cannot be recovered once deleted
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ <p className="text-sm">Are you sure you want to empty the bucket "{bucket?.name}"?</p>
+ </Modal.Content>
+ </ConfirmationModal>
+ )
+}
diff --git a/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerColumn.tsx b/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerColumn.tsx
index aa8040ec032..b2ccb1cf02a 100644
--- a/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerColumn.tsx
+++ b/apps/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerColumn.tsx
@@ -238,11 +238,12 @@ const FileExplorerColumn = ({
column.status !== STORAGE_ROW_STATUS.LOADING && (
<div className="h-full w-full flex flex-col items-center justify-center">
<img
+ alt="storage-placeholder"
src={`${BASE_PATH}/img/storage-placeholder.svg`}
className="opacity-75 pointer-events-none"
/>
- <p className="my-3 opacity-75">Drop your files here</p>
- <p className="w-40 text-center text-sm text-foreground-light">
+ <p className="text-sm my-3 opacity-75">Drop your files here</p>
+ <p className="w-40 text-center text-xs text-foreground-light">
Or upload them via the "Upload file" button above
</p>
</div>
diff --git a/apps/studio/components/ui/ConfirmationModal.tsx b/apps/studio/components/ui/ConfirmationModal.tsx
index 940fdafd810..4a3f41fd176 100644
--- a/apps/studio/components/ui/ConfirmationModal.tsx
+++ b/apps/studio/components/ui/ConfirmationModal.tsx
@@ -67,7 +67,7 @@ const ConfirmationModal = ({
disabled={loading || buttonDisabled}
onClick={onConfirm}
>
- {loading ? buttonLoadingLabel : buttonLabel}
+ {(loading && buttonLoadingLabel) || buttonLabel}
</Button>
</div>
}
diff --git a/apps/studio/data/storage/bucket-empty-mutation.ts b/apps/studio/data/storage/bucket-empty-mutation.ts
new file mode 100644
index 00000000000..2e14636ccb4
--- /dev/null
+++ b/apps/studio/data/storage/bucket-empty-mutation.ts
@@ -0,0 +1,55 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { storageKeys } from './keys'
+
+export type BucketEmptyVariables = {
+ projectRef: string
+ id: string
+}
+
+export async function emptyBucket({ projectRef, id }: BucketEmptyVariables) {
+ if (!projectRef) throw new Error('projectRef is required')
+ if (!id) throw new Error('Bucket name is requried')
+
+ const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/empty', {
+ params: { path: { id, ref: projectRef } },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type BucketEmptyData = Awaited<ReturnType<typeof emptyBucket>>
+
+export const useBucketEmptyMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<BucketEmptyData, ResponseError, BucketEmptyVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<BucketEmptyData, ResponseError, BucketEmptyVariables>(
+ (vars) => emptyBucket(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(storageKeys.buckets(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to empty bucket: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/apps/studio/localStores/storageExplorer/StorageExplorerStore.ts b/apps/studio/localStores/storageExplorer/StorageExplorerStore.ts
index b9900136b62..632ce1df1b2 100644
--- a/apps/studio/localStores/storageExplorer/StorageExplorerStore.ts
+++ b/apps/studio/localStores/storageExplorer/StorageExplorerStore.ts
@@ -4,7 +4,6 @@ import { createClient } from '@supabase/supabase-js'
import { BlobReader, BlobWriter, ZipWriter } from '@zip.js/zip.js'
import { chunk, compact, find, findIndex, has, isEqual, some, uniq, uniqBy } from 'lodash'
import { makeAutoObservable } from 'mobx'
-import { createContext, useContext } from 'react'
import toast from 'react-hot-toast'
import {
@@ -28,14 +27,6 @@ export function useStorageStore() {
return store
}
-/**
- * Deprecated - it's preferable to use the useStorageStore() function above
- */
-export const StorageExplorerContext = createContext(null)
-export const useStorageExplorerStore = () => {
- return useContext(StorageExplorerContext)
-}
-
const CORRUPTED_THRESHOLD_MS = 15 * 60 * 1000 // 15 minutes
const LIMIT = 200
const OFFSET = 0
diff --git a/apps/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx b/apps/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
index ee97c0bd770..e631591593c 100644
--- a/apps/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
+++ b/apps/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
@@ -29,7 +29,6 @@ const PageLayout: NextPageWithLayout = () => {
<p className="text-sm text-foreground-light">Bucket {bucketId} cannot be found</p>
</div>
) : (
- // @ts-ignore
<StorageExplorer bucket={bucket} />
)
) : (