0

I want to trunacate the table 'core_session' by loaded a php script.

How can I fix this, how should this script look like?

2 Answers 2

8

No need to load fancy pantsy models and stuff. It can be done much simpler. The only thing you need is a connection to the database:

require_once('./app/Mage.php');
Mage::app();
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->truncateTable('core_session');

or:

$connection->query('TRUNCATE TABLE `core_session`');

Having such scripts globally accessible in the root of your site is a huge security risk by the way, but we'll save that discussion for later.

0
2

Disclaimer: Truncating table logic is taken from @sv3n's answer

Create a file truncate.php and put it in the root folder. Add below code in that PHP file:

<?php
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
Mage::app();

$model      = Mage::getModel('core/session');
$resource   = $model->getResource();
$connection = $resource->getReadConnection();
/* @see Varien_Db_Adapter_Pdo_Mysql */
$connection->truncateTable($resource->getMainTable());
$connection->changeTableAutoIncrement($resource->getMainTable(), 1);
echo "table is successfully truncated.";

Make sure you replace the $model with your model. Now load the page www.yourdomain.com/truncate.php. You are done.

Also please consider going through Marius's answer.

5
  • Thanks for your reply. What exactly should I change in the $model, when I want to truncate table 'core_session'? Commented Jun 22, 2017 at 12:05
  • 1
    By the way, truncating a table in MySQL will by default reset the auto_increment value to 1 so you don't need to de that manually. Commented Jun 22, 2017 at 12:11
  • 2
    @GielBerkers I am 100% agreeing with you. [to OP]: I updated my answer with right model value. also keeping this script in the root folder is a big risk. [to Giel] : What your code does exactly same as what this code does. Instead of giving table name directly, we are just grabbing that from the model. This is the "Magento way". Even if table name changed in future (which is not going to happen, I know), this code will work. Commented Jun 22, 2017 at 12:19
  • Please try Giel's answer which is simpler than this. Commented Jun 22, 2017 at 12:21
  • Please don't put it in the root folder of your website, it is a huge security risk. Commented Jun 23, 2017 at 10:06

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.