Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated Mar 26, 2026

ファイルを削除する

remove API を使用してストレージバケットからファイルを削除できます。ファイルが ID で保護されている場合、そのファイルを削除できるのはそのファイルを所有するユーザーのみです。

単一ファイルを削除する

Amplify Backend に割り当てられたバケット名を bucket オプションで指定して、特定のバケットから削除操作を実行することもできます。

import { remove } from 'aws-amplify/storage';
try {
await remove({
path: 'album/2024/1.jpg',
// または、path: ({identityId}) => `album/${identityId}/1.jpg`
bucket: 'assignedNameInAmplifyBackend', // Amplify Backend で割り当てられた名前を使用してターゲットバケットを指定
});
} catch (error) {
console.log('Error ', error);
}

または、コンソールからバケット名とリージョンを指定してオブジェクトを渡すこともできます。

import { remove } from 'aws-amplify/storage';
try {
await remove({
path: 'album/2024/1.jpg',
// または、コンソールからバケット名と関連リージョンを指定
bucket: {
bucketName: 'bucket-name-from-console',
region: 'us-east-2'
}
});
} catch (error) {
console.log('Error ', error);
}

フォルダを削除する

フォルダパスを指定することで、フォルダ全体とその内容を削除できます。remove API は自動的にフォルダを検出し、含まれるすべてのファイルのバッチ削除を実行します。

import { remove } from 'aws-amplify/storage';
try {
await remove({
path: 'album/2024/'
});
} catch (error) {
console.error(error);
}

進行状況トラッキング付きフォルダ削除

大きなフォルダの場合、削除の進行状況を追跡し、エラーを処理できます。

import { remove } from 'aws-amplify/storage';
try {
const result = await remove({
path: 'large-folder/',
options: {
onProgress: (fileBatch) => {
console.log(fileBatch);
}
}
});
console.log('Success', result);
} catch (error) {
console.log('Error during folder deletion:', error);
}

キャンセル可能なフォルダ削除

フォルダ削除操作をキャンセルできます。これはユーザーが開始したキャンセルやページから移動する際に役立ちます。

await なしで remove() を呼び出すと、result プロパティと cancel() メソッドを持つキャンセル可能な操作オブジェクトが返されます。これは直接結果を返しますがキャンセルできない await remove() とは異なります。

import { remove } from 'aws-amplify/storage';
let deleteOperation;
// ユーザーが削除ボタンをクリックしたときに削除を開始
function handleDeleteFolder() {
// remove() は { result: Promise, cancel: Function } を返す
deleteOperation = remove({
path: 'user-uploads/large-dataset/',
options: {
onProgress: (fileBatch) => {
updateProgressBar(fileBatch.deleted?.length || 0);
}
}
});
// .result プロパティを通じて Promise にアクセス
deleteOperation.result.then(result => {
console.log('Success', result);
}).catch(error => {
if (error.name === 'CanceledError') {
console.log('Deletion cancelled by user');
} else {
console.log('Error:', error);
}
});
}
// ユーザーがキャンセルをクリックするか、移動する際にキャンセル
function handleCancel() {
if (deleteOperation) {
deleteOperation.cancel();
}
}

さらなる remove オプション

オプションデフォルト説明
bucketstring |
{ bucketName: string;
region: string; }
Amplify 設定のデフォルトバケットとリージョンAmplify Backend で割り当てられたターゲットバケットの名前を表す文字列、またはコンソールからバケット名とリージョンを指定するオブジェクト。

追加のストレージバケットの設定で詳細を確認してください
expectedBucketOwnerstringオプションリクエストされたバケットを所有するアカウント ID。
onProgress(fileBatch: {
deleted?: {path: string}[];
failed?: {path: string; code: string; message: string}[];
}) => void
オプションフォルダ削除の進行状況を追跡するためのコールバック関数。フォルダ削除操作中に各バッチのファイルが処理された後に呼び出されます。