First, you need to create a custom GridFieldComponent for 'Delete All' button - something like below
class GridFieldDeleteAllButton implements GridField_HTMLProvider, GridField_ActionProvider {
protected $targetFragment;
public function __construct($targetFragment = "before") {
$this->targetFragment = $targetFragment;
}
public function getHTMLFragments($gridField) {
$button = new GridField_FormAction(
$gridField,
'deleteall',
_t('TableListField.DELETEALL', 'Delete All'),
'deleteall',
null
);
$button->setAttribute('data-icon', 'cross-circle');
$button->addExtraClass('no-ajax');
return array(
$this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>',
);
}
public function getActions($gridField) {
return array('deleteall');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == 'deleteall') {
return $this->handleDeleteAll($gridField);
}
}
public function handleDeleteAll($gridField, $request = null) {
//Add your own DELETE logic here !
$items=$gridField->getList();
foreach ($items as $item) {
$item->delete();
}
Controller::curr()->redirectBack();
}
}
You then can add this new component to your gridfield under your ModelAdmin extension
public function updateEditForm(&$form) {
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->owner->modelClass));
$gridField->getConfig()->addComponent(new GridFieldDeleteAllButton('before'));
}