6

I created a block and put in some invalid PHP code resulted in entire site not accessible at all.

I'm fairly new to Drupal and I'm not a back-end programmer either. Much appreciated if you can provide a clear step-by-step guide on how to delete that particular block directly from the Database (PhpMyAdmin).

4 Answers 4

7
  • Login into PHPMy Admin with admin credentials
  • Search for the block table; in this table search for the block which you have created
  • Change the value of status column form 1 to 0
  • Delete all the content from the cache_bootstrap table
3
  • Thanks! I ended up following your guide since it's a step by step instruction. Downside is that if there's a lot of rows then would be nice to have a more efficient way of finding the block than going through it manually. Commented Jun 1, 2012 at 4:11
  • 2
    To find rows in a database, you use SELECT; just use the right criteria to match the blocks you are looking for. Unfortunately, Drupal Answers doesn't aim to teach you how to use PHPMyAdmin, since it is off-topic.
    – avpaderno
    Commented Jul 31, 2014 at 21:21
  • This solution does not remove the block at all, it merely disables it. If you need to remove the block, see the answer below. Commented Nov 23, 2017 at 9:33
10

To delete a block programmatically using the Database API, try this:

$module = 'block';
$delta = 1;

// Get the block id (bid) required to delete the correct block from table 'block_custom'
$result = db_select('block', 'b')
         ->fields('b', array('bid'))
         ->condition('module', $module)
         ->condition('delta', $delta)
         ->execute();
$bid = $result->fetchField();

db_delete('block_custom')
  ->condition('bid', $bid)
  ->execute();

db_delete('block')
  ->condition('module', $module)
  ->condition('delta', $delta)
  ->execute();

db_delete('block_role')
  ->condition('module', $module)
  ->condition('delta', $delta)
  ->execute();

When creating custom blocks through the Drupal Block UI, the module name is block and the delta value is an integer. Be sure to use the correct delta value to delete the desired block.

6
  • 1
    Boom. Thanks, Tyler. I'd also add block_node_type to the list. : ) Commented Mar 8, 2016 at 22:52
  • Warning! Using $delta to match against bid in table block_custom is very dangerous as you will delete another block. You should use the bid id from table block instead. Commented Mar 17, 2017 at 12:45
  • Updated the example to fix the problem. Commented Mar 17, 2017 at 12:59
  • 1
    @MilošKroulík: It has been over three years since I posted this, but yes, as I recall it may cause the wrong block to be deleted. We had a large Drupal installation with many modules adding blocks. Commented Jan 30, 2019 at 8:05
  • 1
    Hi, according to documentation api.drupal.org/api/drupal/includes!database!database.inc/… the second parameter of fields() method should be an array otherwise you will get an error: ->fields('b', array('bid')). Commented Apr 4, 2019 at 12:32
3

just use this simple query to delete the block.

delete from block where module = 'MODULE' and title = 'TITLE'

Replace MODULE with the name of the module creating the block, and TITLE with the block title.

If the block doesn't have a title, you could use only delete from block where module = 'MODULE'.

3
  • Thanks for sharing your solution :) What would be the query to search instead of delete? Can I replace delete with search or something like that? Commented Jun 1, 2012 at 4:11
  • yes the query would be SELECT * FROM block WHERE module = 'MODULE' replace MODULE by the module creating the block i.e. search
    – AjitS
    Commented Jun 1, 2012 at 6:47
  • What if I don't know my block name? How does a block name translate into the database? Is everything in lowercase with underscores instead of spaces?
    – alouette
    Commented Jan 7, 2015 at 15:46
0

delete from block where delta = '[DELTA]';

If you created the block programmatically you should be able to recreate the delta value with the same logic you used to create it. It's the value you passed as the KEY to your blocks array for example $blocks['myblock'] -- myblock is the value stored in delta.

On an aside, I personally think its a good idea to prefix this name with your module prefix. When I look in my Blocks table, I can see mymodule_myblock for my delta and there is no ambiguity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.