public function deleteOldFileOnSave(EntityInterface $entity, $oldIdField = 'old_file_id') {
		if (!empty($entity->get($oldIdField)) && $entity->get('model')) {
			$oldEntity = $this->_table->find()
				->contain([])
				->where([
					$this->_table->getAlias() . '.' . $this->_table->getPrimaryKey() => $entity->get($oldIdField),
					'model' => $entity->get('model')
				])
				->first();

			if (!empty($oldEntity)) {
				return $this->_table->delete($oldEntity);
			}
		}

		return false;
	}
public function deleteOldFileOnSave(EntityInterface $entity, $oldIdField = 'old_file_id') {
		if (!empty($entity->get($oldIdField)) && $entity->get('model')) {
			$oldEntity = $this->_table->find()
				->contain([])
				->where([
					$this->_table->getAlias() . '.' . $this->_table->getPrimaryKey() => $entity->get($oldIdField),
					'model' => $entity->get('model')
				])
				->first();

			if (!empty($oldEntity)) {
				return $this->_table->delete($oldEntity);
			}
		}

		return false;
	}
public function deleteOldFileOnSave ( EntityInterface $ entity , $ oldIdField = 'old_file_id' ) { if ( ! empty ( $ entity -> get ( $ oldIdField ) ) && $ entity -> get ( 'model' ) ) { $ oldEntity = $ this -> _table -> find ( ) -> contain ( [ ] ) -> where ( [ $ this -> _table -> getAlias ( ) . '.' . $ this -> _table -> getPrimaryKey ( ) => $ entity -> get ( $ oldIdField ) , 'model' => $ entity -> get ( 'model' ) ] ) -> first ( ) ; if ( ! empty ( $ oldEntity ) ) { return $ this -> _table -> delete ( $ oldEntity ) ; } } return false ; }
Deletes an old file to replace it with the new one if an old id was passed.

Thought to be called in Table::afterSave() but can be used from any other
place as well like Table::beforeSave() as long as the field data is present.
The old id has to be the UUID of the file_storage record that should be deleted.

Table::deleteAll() is intentionally not used because it doesn't trigger
callbacks.

@param \Cake\Datasource\EntityInterface $entity
@param string $oldIdField Name of the field in the data that holds the old id.
@return bool Returns true if the old record was deleted
Deletes an old file to replace it with the new one if an old id was passed .